4

I'm created model in app/models/request/book folder but Book::Request::Status.table_name returns table name "statuses" ("book_request_statuses" - is right table name). How I can get correct table name?

model location

model/
  book/
    request/
      status.rb

model/book/request/status.rb

class Book::Request::Status < ActiveRecord::Base
...
end

config/application.rb

config.autoload_paths += Dir[Rails.root.join('app', 'models', '**', '*.rb')]

If I set self.table_name = "book_request_statuses" then the model will work correctly (in model), but it's not good way :).

sorry for my English is not good

vmamaev
  • 199
  • 5
  • 12

1 Answers1

4

1) Create a module in app/models/book.rb with these lines.

module Book
  def self.table_name_prefix
    'book_'
  end
end

2) Then create another module in app/models/book/request.rb

module Request
  def self.table_name_prefix
    'request_'
  end
end

3) Put the status model inside the app/models/book/request/ directory.

4) Keep all the other files intact.

I hope that works for you.

Spyros
  • 227
  • 2
  • 12
  • 3
    I should add. I just tried calling `rails g model request/book/status` and it generated the items in this answer. It also generated the correct migration. So I would suggest doing that to make it a bit easier :). Cheers. – Andy May 14 '16 at 15:28