Ask A Question

Notifications

You’re not receiving notifications from this thread.

Best Approach for Updating Attributes On Join Table

Mountaindog 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!

Reply

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.

Reply

Thanks Chris for the response. Sorry for the delay, I had pivot to another project for small feature update. I feel I'm close to solving this issue, but I'm drawing a blank on the best approach to submit a form that can update multiple rows / (or ids). I'm using SimpleForm and the view/_form looks like this:

simple_form_for(@author_article, url: article_author_articles_path,  html: {class: 'form-group'}) do |f|
  - if @article.errors.any?
    = render partial: "partials/error_messages", locals: {object: @article}

  - @authors.each do |i|
    .field.text-capitalize
      = f.label i.fullname
      = f.hidden_field :author_ids
    .field
      - i.author_articles.where("author_id = ?", i.id).where("article_id = ?", @article.id).each do |o|
        = f.input :author_sequence_ids, 
          collection: @author_sequences,
          selected: o.author_sequence_id, 
          label: false,
          prompt: "Choose Something"
  .actions
    %p= f.submit 'Save', class: 'btn btn-sm btn-primary text-center'

The form appears like this in the view.

Chris Rock <select><option "Choose Something ... 
Jimmy Fallen <select><option First ... selected
Chuck Norris <select<option Second ... selected 
<button>Save ... 

And of course in the controller (AuthorArticlesController) I'm permitting author_sequence_ids: [], author_ids: [], article_id
The issue I'm having is the dropdown menu does not know the author. Any advice you or anyone else would be welcome!

Reply
Join the discussion
Create an account Log in

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

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

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