Rails Model without a table

后端 未结 5 1080
你的背包
你的背包 2020-12-05 02:24

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can\'t find it on google.

My questi

5条回答
  •  臣服心动
    2020-12-05 02:41

    The answers are fine for 2013 but since Rails 4 all the database independent features of ActiveRecord are extracted into ActiveModel. Also, there's an awesome official guide for it.

    You can include as many of the modules as you want, or as little.

    As an example, you just need to include ActiveModel::Model and you can forgo such an initialize method:

    def initialize(attributes = {})
      attributes.each do |name, value|
        send("#{name}=", value)
      end
    end
    

    Just use:

    attr_accessor :name, :age
    

提交回复
热议问题