1

I'm trying to build a form that allows users to update some records. They can't update every field, though, so I'm going to do some explicit processing (in the controller for now) to update the model vis-a-vis the form. Here's how I'm trying to do it:

Family model:

class Family < ActiveRecord::Base
  has_many :people, dependent: :destroy
  accepts_nested_attributes_for :people, allow_destroy: true, reject_if: ->(p){p[:name].blank?}
end

In the controller

def check
  edited_family = Family.new(params[:family])
  #compare to the one we have in the db
  #update each person as needed/allowed
  #save it
end

Form:

= form_for current_family, url: check_rsvp_path, method: :post do |f|
  = f.fields_for :people do |person_fields|
    - if person_fields.object.user_editable
      = person_fields.text_field :name, class: "person-label"
    - else
      %p.person-label= person_fields.object.name

The problem is, I guess, that Family.new(params[:family]) tries to pull the people out of the database, and I get this:

ActiveRecord::RecordNotFound in RsvpsController#check

Couldn't find Person with ID=7 for Family with ID=

That's, I guess, because I'm not adding a field for family id to the nested form, which I suppose I could do, but I don't actually need it to load anything from the database for this anyway, so I'd rather not. I could also hack around this by just digging through the params hash myself for the data I need, but that doesn't feel a slick. It seems nicest to just create an object out of the params hash and then work with it.

Is there a better way? How can I just create the nested object?

6
  • When you do Family.new you will have a new Family object with no id, but in params[:family] there is perhaps a subhash for "person" that have an id (ID=7) that already are in the db?
    – 244an
    Dec 1, 2012 at 19:06
  • @244an that's right. The family and its nested people were pulled from the DB before they were inserted in the form. The ideas is that the user has edited it and posted it back to check, and now I want to create an object out of the data they posted back.
    – user24359
    Dec 2, 2012 at 6:37
  • I think I don't understand exactly what you want to do, but the problem here seems to be the (nested) "person" => { "id" => 7, ... } in params (and no "id" in family hash). If the params is like that and if I have understand you correctly you can try params['family']['person'].delete('id'), or with keys as Symbols, before the Family.new. Or you have tried that - as I said, I don't understand all you wrote. If this is the solution perhaps you can make that id disappear from the form instead.
    – 244an
    Dec 2, 2012 at 19:50
  • @244an I'll take a crack at rewriting the question in a bit to include more context and be clearer. Removing the id from the form is pretty easy; I just have to add :include_id => false as an argument to form_for. But then I wouldn't be able to match the posted-back people to the ones from my database...I guess I could remove them using delete like you said, but first store the IDs in a variable, but that's getting messy.
    – user24359
    Dec 2, 2012 at 21:50
  • 1
    Or use :include_id => false as you say, and have the id you want in a hidden field with another name instead, then Rails won't try to "use" it.
    – 244an
    Dec 2, 2012 at 21:58

2 Answers 2

1

Rather than instantiating a new Family object with those params, I'd recommend creating a member route for the check rsvp action. The route would take the form of:

resources :families do
  member do
    post 'check_rsvp'
  end
end

The form_for will then automatically pass current_family's id, so that the check action will look like:

def check
  edited_family = Family.find(params[:id])
  # ...
end

While this may seem functionally equivalent to adding the family id parameter yourself, I think it's superior to either that or instantiating a new Family object based on the other params because:

  1. It's more idiomatic (The Rails Way™).
  2. It's less code.
  3. You gain referential transparency for the edited_family object, which reduces the likelihood of subtle bugs that can occur because of an ad hoc instantiation of a new Active Record object based on the attributes of one that's already been persisted.
1
  • I must not be doing a good job of describing what I'm trying to accomplish. I have this family and its nested people in the DB with certain properties. The user is filling out the form in order to edit those properties, like changing the name of one of the people. So the user is posting a modified but incomplete version of the family obj, which I'm going to process and compare to the one I'd get from doing a find. So what I'm really trying to do is to take the non-id properties and fold them into an object which I can play with. So I do need to "deserialize" the family out of the params.
    – user24359
    Dec 2, 2012 at 6:43
0

I ended up taking @244an's suggestion:

class Person < ActiveRecord::Base
  belongs_to :family

  def temp_id
    @temp_id || self.id
  end

  def temp_id=(new_id)
    @temp_id = new_id
  end
end

In the form:

= form_for current_family, url: check_rsvp_path, method: :post, html: {class: "form-inline"} do |f|
  #people-list
    = f.fields_for :people, include_id: false do |person_fields|
      = person_fields.hidden_field :temp_id
      #rest of the form here

Then in my controller:

def check
    @edited_family = Family.new(params[:family])

    current_people = Hash[current_family.people.map{|p| [p.id, p]}]

    @edited_family.people.each do |person|
      current_person = current_people[person.temp_id.to_i]

      next unless current_person

      current_person.name = person.name if current_person.user_editable
      current_person.attending = person.attending
      current_person.save!
    end

    current_family.responded = true
    current_family.save!

  end

The reason I added that field to the model was that I couldn't come up with a good way of using hidden_field to rename the field.

Anyway, it feels superhacky, but it works. What I'd have really wanted was just a way to tell new not to try to match child objects to the DB even if they have ids.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.