On Sunday, September 13, 2015 at 12:00:58 AM UTC-7, Jikku Jose wrote: > > Excuse me for the basic question. I am trying to do tags for a blog post, > this is what I did: > > 1. Added a migration for creating the table: posts_tags (Which stores > post_id and tag_id) > 2. Added many_to_many in Post and Tag models. > 3. Create a model PostsTags for updating the associating posts_tags table. > > It worked well. I was able to retrieve: Post.first.tags to get all the > tags associated with the first post and vice-versa. > > But I am not sure, I had to do the third step? To me, that should have > been handled automatically? I was assuming an API like this: > > Post.new(title: 'Sample Title', body: 'A simple sample blog', tags: > [Tag.first]) > > But now, I am depending on PostsTags to add a new tag; is this the > expected workflow? >
If the posts_tags table only has foreign keys to Post and Tag, then you generally don't want to have a separate model for it. You can do: post = Post.create(title: 'Sample Title', body: 'A simple sample blog') post.add_tag(Tag.first) If you want to setup the association from the post to the tag before creating the post in the database, you may want to look at the association_pks, delay_add_association, nested_attributes, and/or instance_hooks plugins. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
