开发者

How to put many Model classes together in one Module to use in Rails

开发者 https://www.devze.com 2023-02-05 17:23 出处:网络
In rails i have a lot of tables and need to define a lot of Model classes to use for controller, but i want to put all the model classes into one module file, and then make controller to use the model

In rails i have a lot of tables and need to define a lot of Model classes to use for controller, but i want to put all the model classes into one module file, and then make controller to use the model class in the module, but i don't kno开发者_高级运维w how to do it.

Could someone give me help on the problem? Appreciate your help very much.


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

You can also namespace the controllers.

Edit:

lib/widgets.rb
module Widgets
  class BlueWidget
  end

  class RedWidget
  end
end

controller:
require 'lib/widgets'
def index
  @widgets = Widgets::BlueWidget.all
end

Is that what you mean?


You can also generate models directly in subdirectorys and get them im modules:

rails g model user/likes name:string like:boolean

and you will get your generated files.

The generated model would be in app/models/user/likes.rb

class User::Likes < ActiveRecord::Base
end
0

精彩评论

暂无评论...
验证码 换一张
取 消