# Field update, best practice

**URL:** <https://www.ruby-forum.com/t/field-update-best-practice/182243>\
**Category:** Rails\
**Created:** [January 25, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/field-update-best-practice/182243 "2010-01-25T16:43:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [January 25, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/field-update-best-practice/182243/1 "2010-01-25T16:43:16Z")

</div>

Hi everybody… I’m stuck with a strange (stupid) problem I can’t  
understand.

I’m trying to do this in the articles\_controller:

def updatecollection  
@arts = Article.find\_by\_solr(“14 MAY 91”).results

```
@arts.each do |doc|
  @sub = doc.title[15,150]
   # Updating one record:
   doc.title = @sub
  doc.save

end

```

end

It loads but i doesn’t update the article…the strange fact is that if  
I do the same process for a single doc in the console it works… so  
what’s wrong? something in the .each do?  
thanks!

(any different solution it’s fine for me 😉 )

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [January 25, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/field-update-best-practice/182243/2 "2010-01-25T17:00:14Z")

</div>

2010/1/25 adedip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> # Updating one record:
> doc.title = @sub
> doc.save
> 
> ```

Test the return value from save here to see if it succeeded. Perhaps  
it is failing validation.

Colin

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [January 25, 2010, 6:37pm UTC](https://www.ruby-forum.com/t/field-update-best-practice/182243/3 "2010-01-25T18:37:18Z")

</div>

My Guess is that try eliminating the results  
@arts = Article.find\_by\_solr(“14 MAY 91”).results  
from this and try using  
@arts = Article.find\_by\_solr(“14 MAY 91”)

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [January 25, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/field-update-best-practice/182243/4 "2010-01-25T18:45:40Z")

</div>

On 25 Gen, 16:57, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > @arts.each do |doc|  
> > @sub = doc.title[15,150]  
> > # Updating one record:  
> > doc.title = @sub  
> > doc.save
> 
> Test the return value from save here to see if it succeeded. Perhaps  
> it is failing validation.
> 
> Colin

but I get true doing it on console…could it be different on the app  
controller?  
anyway how do i test it? something like:

if doc.save  
puts “true”

?

thx

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [January 25, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/field-update-best-practice/182243/5 "2010-01-25T21:25:29Z")

</div>

2010/1/25 adedip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> but I get true doing it on console…could it be different on the app  
> controller?  
> anyway how do i test it? something like:
> 
> if doc.save  
> puts “true”

No, have a look at the Rails Guide on debugging. There are various  
options, I would use ruby-debug and break in at that point.

Colin
