# Updating referenced table

**URL:** <https://www.ruby-forum.com/t/updating-referenced-table/150179>\
**Category:** Rails\
**Created:** [October 5, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/updating-referenced-table/150179 "2008-10-05T13:41:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![katsuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@katsuo](https://www.ruby-forum.com/u/katsuo)\
**Post date:** [October 5, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/updating-referenced-table/150179/1 "2008-10-05T13:41:43Z")

</div>

I have the following two tables books and records

fields for books table: id, name  
fields for records table:id, name, status

I would like to insert value ‘read’ to status field in records table  
when name matches name in books table. Tried the following but no luck.  
I would appreciae if someone can lead me to the right direction.

def update\_status  
@books=Book.find(:all)

for book in @books  
Record.update\_attributes  
(:status=\>‘read’,:conditions[‘records.name=?’,books.name])  
end

end

---

<div class="post-metadata">

**Author:** ![katsuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@katsuo](https://www.ruby-forum.com/u/katsuo)\
**Post date:** [October 5, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/updating-referenced-table/150179/2 "2008-10-05T17:01:20Z")

</div>

- Katsuo I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2008-10-05 13:41:43  
+0200]:

> def update\_status  
> @books=Book.find(:all)
> 
> for book in @books  
> Record.update\_attributes

I think you should use ActiveRecord.update\_all instead of  
ActiveRecord#update\_attributes, which is an instance method.

Jan

> ```
> (:status=>'read',:conditions['records.name=?',books.name])
> 
> ```
> 
> end
> 
> ## end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
jan=callcc{|jan|jan};jan.call(jan)

---

<div class="post-metadata">

**Author:** ![katsuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@katsuo](https://www.ruby-forum.com/u/katsuo)\
**Post date:** [October 6, 2008, 4:48am UTC](https://www.ruby-forum.com/t/updating-referenced-table/150179/3 "2008-10-06T04:48:47Z")

</div>

I got the result with the following:

def update\_status  
@books=Book.find(:all)

for book in @books  
Record.update\_all(“status=‘read’ where name=’#{book.name}’”)  
end

end

Many thanks FB
