Creating relationship between two model instances
- by Lowgain
This is probably pretty simple, but here:
Say I've got two models, Thing and Tag
class Thing < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :things
end
And I have an instance of each. I want to link them. Can I do something like:
@thing = Thing.find(1)
@tag = Tag.find(1)
@thing.tags.add(@tag)
If not, what is the best way to do this? Thanks!