4
votes

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

1
I think there is no other answer to your question.. Just specify table name in the model and it works like you want. - BvuRVKyUVlViVIc7
ok, thank you. Rails 3 don't have this problem... - vmamaev

1 Answers

4
votes

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.