I’m just wondering if someone could explain to me how to create a relationship (programmatic example would be helpful, so I can test in rails console) between two objects where the relationship is defined as a has_many :through that has additional attributes. The objects are defined as below:
class Item < ActiveRecord::Base
has_many :collections, :through => :collection_items
end
class Collection < ActiveRecord::Base
has_many :items, :through => :collection_items
end
class CollectionItem < ActiveRecord::Base
belongs_to :collection
belongs_to :item
attr_accessible :collection_id, :item_id, :quantity
end
Try this:
Just replace the ‘
Item.first‘ and ‘Collection.first‘ with whatever logic you have to get the right item and collection.