Creating relationship between two model instances
Posted
by Lowgain
on Stack Overflow
See other posts from Stack Overflow
or by Lowgain
Published on 2010-05-12T03:57:20Z
Indexed on
2010/05/12
4:04 UTC
Read the original article
Hit count: 231
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!
© Stack Overflow or respective owner