Rails 4模型子文件夹 [英] Rails 4 model subfolder
问题描述
我在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
...
end
config/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
end
2)然后在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
end
3)将状态模型放入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模型子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!