Update_attribute... only ONE


#1

Hi,

I was reading the messages at
http://wrath.rubyonrails.org/pipermail/rails/2005-December/006969.html
http://wrath.rubyonrails.org/pipermail/rails/2005-December/006969.html,
Subject: [Rails] update_attribute send updates every field, why?

Well, I am having the same problem, and I do have a binary column where
I am
storing files. Also, I am calling a web service so I can send the binary
file (only this column) to the SQL Server database, making a call again
would be kind of expensive.

Is there a way to update only one column instead of all of them?

Thanks you,
Fernanda


#2

I wanted to followup on this with a solution I found. While reading
the Agile Web D. book, I found that it referenced updating
single attributes with the “find_by_sql” in where you will only
update columns that you select. Since I have a :through association,
I do not think that method was available to me.

So in order to update a single attribute to a model object, I limited
the :select option of the standard find method. So it ended up
looking something like this. I used fully qualified column names in
the select statement. I found this worked well.

 obj = User.bla.find(params[:id], :select => "bla.id, bla.title")
 obj_title = params[:title]
 obj.title = obj_title
 if obj.save!
   # etc...
 end

#3

Ken C. wrote:

I wanted to followup on this with a solution I found. While reading
the Agile Web D. book, I found that it referenced updating
single attributes with the “find_by_sql” in where you will only
update columns that you select. Since I have a :through association,
I do not think that method was available to me.

So in order to update a single attribute to a model object, I limited
the :select option of the standard find method. So it ended up
looking something like this. I used fully qualified column names in
the select statement. I found this worked well.

 obj = User.bla.find(params[:id], :select => "bla.id, bla.title")
 obj_title = params[:title]
 obj.title = obj_title
 if obj.save!
   # etc...
 end

Nice trick, I’ll have to remember that one.