Why won't Rails update my object attributes?

Here’s the issue: When I submit a form for updating,
@person.update_attributes doesn’t work. But the next nested clause for
updating images DOES work. How is that possible? However no matter
what I do the Person object will not change its attributes.

def edit
@person = Person.find(params[:id])
if request.post?
@person.update_attributes(params[:person])

    #image saving stuff
    unless params[:image][:data].blank?
      if @person.image
        @image = @person.image
        @image.data = params[:image][:data]
        @image.save
      else
        @image = Image.create(:data => params[:image][:data])
        @image.illustratable = @person
        @image.save
      end
    end

  flash[:notice] = 'Info successfully updated.'
  redirect_to :action => 'show', :id => @person
end

end

—Development.log—
Processing PeopleController#edit [POST]
Session ID: fa1b778f9ca22e894140b08372310404
Parameters: {“commit”=>“Edit”, “action”=>“edit”, “id”=>“2”,
“controller”=>“people”, “image”=>{“data”=>#StringIO:0x4c22474},
“person”=>{“occupation”=>"", “schools”=>"",
“religious_preference”=>“Agnosticism”, “dislikes”=>"",
“birthdate(1i)”=>“1988”, “gender”=>“male”, “birthdate(2i)”=>“12”,
“is_smoker”=>“0”, “birthdate(3i)”=>“13”, “first_name”=>“Test”,
“homepage_url”=>"", “languages”=>"— “, “last_name”=>“User Three”,
“likes”=>”", “nationality”=>"", “hometown”=>"",
“email”=>“[email protected]”}}
e[4;36;1mPerson Load (0.016000)e[0m e[0;1mSELECT * FROM people WHERE
(people.id = ‘2’) LIMIT 1e[0m
e[4;35;1mPerson Columns (0.000000)e[0m e[0mSHOW FIELDS FROM
peoplee[0m
e[4;36;1mSQL (0.000000)e[0m e[0;1mBEGINe[0m
e[4;35;1mPerson Load (0.000000)e[0m e[0mSELECT * FROM people WHERE
(people.email = ‘[email protected]’ AND people.id <> 2) LIMIT 1e[0m
e[4;36;1mSQL (0.000000)e[0m e[0;1mCOMMITe[0m
e[4;35;1mImage Columns (0.000000)e[0m e[0mSHOW FIELDS FROM
imagese[0m
e[4;36;1mImage Load (0.016000)e[0m e[0;1mSELECT * FROM images WHERE
(images.illustratable_id = 2 AND images.illustratable_type = ‘Person’)
LIMIT 1e[0m
e[4;35;1mSQL (0.000000)e[0m e[0mBEGINe[0m
e[4;36;1mSQL (0.000000)e[0m e[0;1mCOMMITe[0m

On 12/15/06, Taylor S. [email protected] wrote:

Here’s the issue: When I submit a form for updating,
@person.update_attributes doesn’t work. But the next nested clause for
updating images DOES work. How is that possible? However no matter
what I do the Person object will not change its attributes.

My first guess is that a validation is failing. What does the
update_attributes call return?

Good call on the validations being the cause. These two validations
were causing the update to silently fail:

validates_inclusion_of :so, :in => SEXUAL_ORIENTATIONS, :allow_nil =>
true
validates_inclusion_of :rp, :in => RELIGIONS, :allow_nil => true

However, these required validations pass:

validates_inclusion_of :gender, :in => GENDERS
validates_inclusion_of :role, :in => ROLES

And yes I have put “require constants” in my People.rb. Does anyone
have any ideas as to why my :allow_nil validations are failing?
:allow_nil DOES work doesn’t it?

Taylor S. wrote:

Good call on the validations being the cause. These two validations
were causing the update to silently fail:

validates_inclusion_of :so, :in => SEXUAL_ORIENTATIONS, :allow_nil =>
true
validates_inclusion_of :rp, :in => RELIGIONS, :allow_nil => true

However, these required validations pass:

validates_inclusion_of :gender, :in => GENDERS
validates_inclusion_of :role, :in => ROLES

And yes I have put “require constants” in my People.rb. Does anyone
have any ideas as to why my :allow_nil validations are failing?
:allow_nil DOES work doesn’t it?

you might want to add a conditional around update_attributes to catch
the validation:

if request.post?
if @person.update_attributes(params[:person])

    #image saving stuff
    unless params[:image][:data].blank?
      if @person.image
        @image = @person.image
        @image.data = params[:image][:data]
        @image.save
      else
        @image = Image.create(:data => params[:image][:data])
        @image.illustratable = @person
        @image.save
      end
    end

  flash[:notice] = 'Info successfully updated.'
  redirect_to :action => 'show', :id => @person
 else

   render :action => "edit"
 end
end

make sure you have
<%= error_messages_for :person %>

in your edit view

–jake

On 12/16/06, Taylor S. [email protected] wrote:

validates_inclusion_of :gender, :in => GENDERS
validates_inclusion_of :role, :in => ROLES

And yes I have put “require constants” in my People.rb. Does anyone
have any ideas as to why my :allow_nil validations are failing?
:allow_nil DOES work doesn’t it?

My guess here would be that the values are ‘’ (empty string), and not
nil.