ruby on rails - Adding a record to a has_many_and_belongs_to association? -
i have 2 rails models of form:
class user < activerecord::base has_many_and_belongs_to :tags end class tag < activerecord::base has_many_and_belongs_to :users end
i have migration creates tags_users table.
how add record tags_users table? (tags , users unique)
i've trying doing
users.tag_ids << new_tag
but not seem work. pointers welcome
there various ways achieve that, e.g:
u = user.first t = tag.create(name: 't') u.tag_ids << t.id # or: u = user.first t = tag.new(name: 't') u.tags << t
you can't call collection_singular_ids
(tag_ids
in case) on collection. worth noting having habtm association, not have access join model explicitly (e.g taguser
).
Comments
Post a Comment