在rails中我有很多表,需要定义很多模型类用于 Controller ,但是我想把所有模型类放到一个模块文件中,然后让 Controller 使用模块中的模型类,但是我不知道该怎么做。
有人可以帮我解决这个问题吗?非常感谢您的帮助。
最佳答案
app/models/widgets/blue_widget.rb
class Widgets::BlueWidget < ActiveRecord::Base
// etc.
end
app/controllers/blue_widget_controller.rb
def index
@widgets = Widgets::BlueWidget.all
end
您还可以命名 Controller 。
编辑:
lib/widgets.rb
module Widgets
class BlueWidget
end
class RedWidget
end
end
controller:
require 'lib/widgets'
def index
@widgets = Widgets::BlueWidget.all
end
你是这个意思吗?
关于ruby-on-rails - 如何将多个模型类放在一个模块中以在 Rails 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756438/