# A question about updating selected columns only

**URL:** <https://www.ruby-forum.com/t/a-question-about-updating-selected-columns-only/107846>\
**Category:** Rails\
**Created:** [June 26, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/a-question-about-updating-selected-columns-only/107846 "2007-06-26T20:29:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_De\_la\_Cruz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Adrian\_De\_la\_Cruz](https://www.ruby-forum.com/u/Adrian_De_la_Cruz)\
**Post date:** [June 26, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/a-question-about-updating-selected-columns-only/107846/1 "2007-06-26T20:29:43Z")

</div>

Hello guys,  
Seems like I messed up the first time I tried to send this to the list,  
so here it is in the Ruby forums 🙂

I have a little problem, I don’t know how to update a few selected  
columns in my model. Here is a little example to illustrate my  
dilemma:

Suposse I have a model called Book, which have 2 fields (plus the id  
one): title and author.

Now if I type this in the console:

> > b1 = Book.new ( :title =\> “title1”, :author =\> “author1” )  
> > b1.save

After those commands I have a single record sitting in my book’s  
database.

Now suposse a second user gets access to the database at the same time  
as the first one. So we have 2 consoles running at the same time, now  
I will present the commands typed at each console in the exact order  
(I know there is no need for different variable names, but for  
clarity’s sake…):

# Console1

b1 = Book.find(:first)

# Console2

b2 = Book.find(:first)

# Console1

b1.update\_attribute( :title, “from console1” )

# Console2

b2.update\_attribute( :author = “from console2” )

Now what I would expecto to have is a record like this:  
Book.title = “from console1”  
Book.author = “from console2”

But instead I have:  
Book.title = “title1”  
Book.author = “from console2”

Why do I expect that? because I’m updating only THAT attribute, not  
trying to save the whole row, if I wanted to save the whole row I  
would use something like this after fetching the row with find(:first)  
:

b1 = Book.find(:first)  
b1.title = “from console1”  
b1.save

Now intuitively I know those 3 lines above fetch the entire row and  
saves the entire row to the database. But with the other examples I  
gave earlier I would expect them to fetch the entire row and only  
update the selected attribute.

How can I accomplish the behavior I desire?  
(It’s not necessary to fetch the whole row, maybe could I only fetch  
the attributes I intend to modify?)

Thanks a lot for reading this 😃

---

<div class="post-metadata">

**Author:** ![Adrian\_De\_la\_Cruz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Adrian\_De\_la\_Cruz](https://www.ruby-forum.com/u/Adrian_De_la_Cruz)\
**Post date:** [June 27, 2007, 5:37am UTC](https://www.ruby-forum.com/t/a-question-about-updating-selected-columns-only/107846/2 "2007-06-27T05:37:05Z")

</div>

I don’t know the answer to your question but I would watch the log files  
and look at the db statements being issued. Then I would try different  
things and see how that affected the db statements.

An instance does not need to have all the fields. So, selecting only  
the field that you want to update should work.

The other idea which you can probably do with Ruby (but I don’t know  
how) is to fetch the whole thing and delete the attributes (fields) that  
you do not want to update.

Good luck.
