Add association (

This should work in Rails 3.2 and Rails 4:

post.association(:tags).add_to_target(Tag.first)

See this gist: https://gist.github.com/betesh/dd97a331f67736d8b83a

Note that saving the parent saves the child and that child.parent_id is NOT set until you save it.

EDIT 12/6/2015:
For a polymorphic record:

post.association(:tags).send(:build_through_record, Tag.first)
# Tested in Rails 4.2.5

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)