Forum: Ruby on Rails update_attributes not working in self.up migration

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-05-16 09:01
i have a migration script that looks like this:

  def self.up
   rename_column :books, :finalizing, :progress_step
   change_column :books, :progress_step, :integer, :length=>2
   Book.find(:all).each do |book|
      book.update_attributes(:progress_step=>3) if book.published_at
      book.update_attributes(:progress_step=>1) if !book.published_at
      book.update_attributes(:views=>0) if !book.views
   end
   puts "rebuilding ferret index"
   Rake::Task["ferret:rebuild_index"].invoke
  end

When I checked the database fields, it turns out that progress step is
still NULL.

Do you guys have any  idea why this could be?

Do I need to do book.reload or something?
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-05-16 12:27
(Received via mailing list)
On May 16, 2007, at 3:01 AM, Aryk Grosz wrote:
>    puts "rebuilding ferret index"
>    Rake::Task["ferret:rebuild_index"].invoke
>   end
>
> When I checked the database fields, it turns out that progress step is
> still NULL.
>
> Do you guys have any  idea why this could be?
>
> Do I need to do book.reload or something?

Look at the difference in what "update_attributes" (with an "s") does
compared to "update_attribute".  You want the singular form for what
you're doing.  The plural version replaces the entire attributes
property of the object.

-Rob


Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-05-16 17:35
(Received via mailing list)
On 5/16/07, Aryk Grosz <rails-mailing-list@andreas-s.net> wrote:
>    end
>    puts "rebuilding ferret index"
>    Rake::Task["ferret:rebuild_index"].invoke
>   end
>
> When I checked the database fields, it turns out that progress step is
> still NULL.
>
> Do you guys have any  idea why this could be?
>
> Do I need to do book.reload or something?

You need to call

   Book.reset_column_information

There's an example of this in the API docs for
ActiveRecord::Migration. Look for "Using a model after changing its
table"
813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-05-16 17:40
(Received via mailing list)
On 5/16/07, Rob Biedenharn <Rob@agileconsultingllc.com> wrote:
>
> Look at the difference in what "update_attributes" (with an "s") does
> compared to "update_attribute".  You want the singular form for what
> you're doing.  The plural version replaces the entire attributes
> property of the object.
>

I'm pretty sure that's not correct. You can update only a subset of
the attributes with this method.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-05-16 22:21
(Received via mailing list)
On May 16, 2007, at 11:39 AM, Bob Showalter wrote:
> On 5/16/07, Rob Biedenharn <Rob@agileconsultingllc.com> wrote:
>> Look at the difference in what "update_attributes" (with an "s") does
>> compared to "update_attribute".  You want the singular form for what
>> you're doing.  The plural version replaces the entire attributes
>> property of the object.
>>
>
> I'm pretty sure that's not correct. You can update only a subset of
> the attributes with this method.

You're correct, Bob.  I let my old habits get the better of me and
assumed that self.attributes=attributes;save was doing a simple
writer.  Silly me!

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-05-16 22:41
Yeah I was a little worried, because I like passing my updates as
hashes,

I tend to use update_attributes A LOT, even on single attribute
assignments.

It seems like that only real difference is that update_attributes saves
with validation.

Is their a huge performance decrease with using update_attributes since
it calls the attributes= EXCEPT for the fact that it runs validations of
course?

Be very careful with your answer, I might get a heart attack having to
modify all my updates. =)

Aryk
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-05-16 22:54
(Received via mailing list)
Aryk Grosz wrote:
> Yeah I was a little worried, because I like passing my updates as
> hashes,
>
> I tend to use update_attributes A LOT, even on single attribute
> assignments.
>
> It seems like that only real difference is that update_attributes saves
> with validation.

update_attributes will also not update protected attributes.

> Is their a huge performance decrease with using update_attributes since
> it calls the attributes= EXCEPT for the fact that it runs validations of
> course?

It depends on how expensive validation is, and how important it
is to have a sanity check on the internal updates you make to
form and non-form attributes.

--
We develop, watch us RoR, in numbers too big to ignore.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-05-16 23:01
Not quite sure what you mean by the internal updates to form and
non-form attributes.

BTW, how can an attribute in the database be protected? And what does
that exactly mean?

Mark Reginald James wrote:
> Aryk Grosz wrote:
>> Yeah I was a little worried, because I like passing my updates as
>> hashes,
>>
>> I tend to use update_attributes A LOT, even on single attribute
>> assignments.
>>
>> It seems like that only real difference is that update_attributes saves
>> with validation.
>
> update_attributes will also not update protected attributes.
>
>> Is their a huge performance decrease with using update_attributes since
>> it calls the attributes= EXCEPT for the fact that it runs validations of
>> course?
>
> It depends on how expensive validation is, and how important it
> is to have a sanity check on the internal updates you make to
> form and non-form attributes.
>
> --
> We develop, watch us RoR, in numbers too big to ignore.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-05-16 23:02
ooh woops, nevermind about the protected attribute question
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-05-16 23:27
(Received via mailing list)
Aryk Grosz wrote:
> Not quite sure what you mean by the internal updates to form and
> non-form attributes.

Updates to attributes, both those normally used in posted client forms
and internal-only attributes, unrelated to form postings.

> BTW, how can an attribute in the database be protected? And what does
> that exactly mean?

See: http://api.rubyonrails.org/classes/ActiveRecord/Ba...

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.