Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:32:20+00:00 2026-06-15T18:32:20+00:00

I am having the hardest time getting this to properly render and update. What

  • 0

I am having the hardest time getting this to properly render and update. What is going on, is that I have a many to many association, with the association joining table containing multiple attributes. A case can have many screens, a screen many cases, and a case’s screen can be ordered for each case. One other strange item that is occurring with my current setup, is that the association table, upon each time of submiting the form, is not clearing out the old association values. The new values are being appended to the table.

Classes:

class Case < ActiveRecord::Base
    attr_accessible :cases_screens_attributes

    has_many :cases_screens
    has_many :screens, :through => :cases_screens

    accepts_nested_attributes_for :cases_screens, 
                                  :allow_destroy => true,
                                  :reject_if => lambda { |p| p[:screen_id].nil? }
end

class Screen < ActiveRecord::Base
    has_many :cases_screens
    has_many :cases, :through => :cases_screens
end

class CasesScreen < ActiveRecord::Base
    attr_accessible :order

    belongs_to :case
    belongs_to :screen
end

Controller:

# GET /cases/1/edit
def edit
    @case = Case.find(params[:id])
    @cases_screens = @case.cases_screens.order("cases_screens.[order] ASC")
    @screens = Screen.where({:active => true})
end

# PUT /cases/1
# PUT /cases/1.json
def update
    @case = Case.find(params[:id])
    @cases_screens = @case.cases_screens.order("cases_screens.[order] ASC")
    @screens = Screen.where({:active => true})

    respond_to do |format|
      if @case.update_attributes(params[:case])
        format.html { redirect_to case_path(@case), :notice => t(:case_successfully_updated) }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @case.errors, :status => :unprocessable_entity }
      end
    end
end

I have tried many attempts at using fields_for to no avail. This is what I currently have in my _form.erb.html file. The final goal is to, on the case form see a list of all screens with a checkbox next to each screen. Each checkbox also has an associated input for “order”. Upon submission, the cases_screens table will be properly populated (old values removed if no longer selected).

<div class="controls">

  <% @screens.each do |s| %>

    <label>
      <%= check_box_tag("case[cases_screens_attributes][#{s.id}][screen_id]", s.id, @case.screen_ids.include?(s.id)) %>
      <%= s.name %>
    </label>
      <%= select_tag("case[cases_screens_attributes][#{s.id}][order]", options_for_select(1..@screens.size, 2)) %>
  <% end %>

</div>

I have been banging my head against the wall for a few days now on this one, and I just cannot seem to get it right. Any help out there would be greatly appreciated.

Additional Note:

I use simple form, I just did the following:

<%= f.association :screens %>

and everything works as expected (sans order attribute update). The association table is properly cleaned up. This is, of course, pointing me in the direction of nested attributes as the culprit, but I am unsure.

Update Wednesday 10:45
I am now convinced that the issue that is happening here is a lack of having the cases_screens_id present in the form. Normally during cleanup, rails will use this ID to clean up previous entries, then populate the new. Since I am looping over @screens, rather then the currently present cases_screens (associated to the case), I have no way of getting the id without a query going over each row.

I guess for now, my solution is going to write a before_save method into cases_screens that will clean up the current entries for cases_screens, then allow the new values to populate.

Update 1:15
I have gotten a little bit closer with at least one of the items so that records are only inserted if a checkbox is checked. I have added the updated :reject_if to my Case class above. I still have no solution as to why values are only appended to table, and why previous values are not removed.

Update 4:00
My latest attempt included a hidden field that contained the cases_screens ID if the record had only been set. This required me to make a subquery in the loop to get the current cases_screen_id. Along with the :reject_if parameter, values are now properly being updated. However, Currently set values can no longer be removed.

There is no way I am doing this right, it doesn’t feel rails-like. Would this scenario be one of the ones where I should just process the data manually in the controller?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:32:21+00:00Added an answer on June 15, 2026 at 6:32 pm

    At this time, I am chalking this up to a design issue.

    I am now using

      <% @case.cases_screens.each do |cs| %>
        <%= hidden_field_tag("case[cases_screens_attributes][#{cs.id}][id]", cs.id) %>
        <%= hidden_field_tag("case[cases_screens_attributes][#{cs.id}][screen_id]", cs.screen_id) %>
        <%= hidden_field_tag("case[cases_screens_attributes][#{cs.id}][_destroy]", 1) %>
        <label><%= check_box_tag("case[cases_screens_attributes][#{cs.id}][_destroy]", 0, @case.cases_screen_ids.include?(cs.id)) %> <%= cs.screen.name %></label>
        <%= text_field_tag("case[cases_screens_attributes][#{cs.id}][ordinality]", cs.ordinality) %>
      <% end %>
    

    This causes only the currently set case/screen linkups to show up. The user will need to add specific screens to the case via a seperate screen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the hardest time getting this to work. I'm trying to copy a
So i have been having the hardest time getting my iphone application approved. And
I am having the hardest time figuring this out, even though it is one
Check this link! I am having the hardest time trying to figure out why
I'm having the hardest time getting Eclipse to connect and reverse engineer from a
I'm having the hardest time with this. I don't even understand the error messages
I am having the hardest time trying to figure out this (should be) simple
I'm having the hardest time trying to convert this date from an API to
I am having the hardest time with my views and formsets saving. I have
I'm having the hardest time getting Mobiscroll to fire off any of the documented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.