All threads / Best Approach for Updating Attributes On Join Table

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Best Approach for Updating Attributes On Join Table

Craig Hoffman asked in Rails

I have a view (show) that display's an Article. The Article has several Author[s]. The Article and Author models look like this:

Associations:

Article Model
has_many author_articles
has_many articles,  through: :author_articles 

Author Model
has_many author_articles
has_many author,  through: :author_articles 

AuthorArticle Model
belongs_to article
belongs_to author

The join table (AuthorArticle) contains an article, author id columns, and another column called position_id. The Position model is also a has_many through. It looks like this:

Associations:

Position Model
has_many author_articles
has_many positions,  through: :author_articles 

Author Model
has_many author_articles
has_many author,  through: :author_articles 

AuthorArticle Model
belongs_to article
belongs_to author
belongs_to position

Here's what I would like to do and I am not exactly sure how to implement it. I would like to click on the author's name from the article show view, open a modal (already done) and have a form (SimpleForm) where the user can select a position (first, second, third, etc...) and press submit. The order of the author is important. Yes, there can be several "first/lead" authors. The form post would update the join table (AuthorArticle) position_id column with the position selected. Authors are added when a new article is created or updated.

Does this make sense? Let me know if you need more context? Something doesn't feel right about this approach and I'm open to suggestions/feedback/ideas.

Thanks!

You can add a position:integer to your AuthorArticle model to keep track of the order.

The acts_as_list gem can help you order them, but that wouldn't allow for duplicates where you might have several "lead authors". You can probably do without it.

The modal would be for an AuthorArticle object and would have two fields for author and position.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,311+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.