I’m a newbie so this may be wrong. feel free to correct me. Just writing it here so I can remember it:
I found lots of info on the web on how to create a HABM relationship but I could find very little info on how to
create records using this relationship.
This is my workflow so far.
My example is a very simple data model. blog posts (Post) and tags (tag) which have a many to many relationship.
I just want to be able to create a post with the associated tags with it. data model & migration have already been created.
Obviously this can be done with far fewer steps, but this is the rere method:
use .new to create instances then fill in data. then .save to save to database after fields are defined:
post = Post.new
tag = Tag.new
post.title = “Title of Post blah bla”
post.body = “body of post blah blah blah blah. lapse.”
post.posting_status = “Published”
tag.name = “tagulard”
KEY PART:
post.tags < < tag
then just save
post.save (i think this also saves the tag)
to access it it later can just show the post:
post = Post.last
then post.tags to see them