I want to turn Model.attribute into Attribute.models.
e.g. I currently have Model.first.attribute => "string" and I’d like to add belongs_to :attributes into the Model.rb, and then after creating Attribute.rb, add has_many :models
I’ve added a column into the Model table for :attribute_id, and I am trying to do something like
Model.each do |m|
a = Attribute.find_or_create_by_name(m.attribute)
m.update_attribute("attribute_id", a.id)
end
Is this the right way to accomplish this?
If Model
has_many :attributes(and Attributebelongs_to :model), then there should be amodel_idin the Attribute model.Then you could do
UPDATE
What you did seems to be OK for me then. The only problem is you’d have a column
attributeand another on namedattribute_idwith abelongs_toin Model . I don’t know how ActiveRecord would react when you’d type Model.first.attribute.