我在app/models/request/book文件夹中创建了模型,但是Book :: Request :: Status.table_name返回表名"statuses"("book_request_statuses"-是正确的表名).如何获得正确的表名?
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/ book/ request/ status.rb模型/书/请求/状态.rb
class Book::Request::Status < ActiveRecord::Base ... endconfig/application.rb
config.autoload_paths += Dir[Rails.root.join('app', 'models', '**', '*.rb')]如果我设置self.table_name ="book_request_statuses",则该模型可以正常工作(在模型中),但这不是好方法:).
If I set self.table_name = "book_request_statuses" then the model will work correctly (in model), but it's not good way :).
对不起,我的英语不好
推荐答案1)使用以下几行在app/models/book.rb中创建一个模块.
1) Create a module in app/models/book.rb with these lines.
module Book def self.table_name_prefix 'book_' end end2)然后在app/models/book/request.rb
2) Then create another module in app/models/book/request.rb
module Request def self.table_name_prefix 'request_' end end3)将状态模型放入app/models/book/request/目录中.
3) Put the status model inside the app/models/book/request/ directory.
4)保留其他所有文件.
4) Keep all the other files intact.
我希望对您有用.
更多推荐
Rails 4模型子文件夹
发布评论