Rails 4模型子文件夹

编程入门 行业动态 更新时间:2024-10-17 13:30:07
本文介绍了Rails 4模型子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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模型子文件夹

本文发布于:2023-10-31 13:24:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   模型   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!