Forum: Ruby on Rails updating referenced table

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.
2f1b77757fbee66ef826844b647a8b4c?d=identicon&s=25 Katsuo Isono (katsuo)
on 2008-10-05 13:41
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
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2008-10-05 17:01
(Received via mailing list)
* Katsuo Isono <rails-mailing-list@andreas-s.net> [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/.
>
>
--
jan=callcc{|jan|jan};jan.call(jan)
2f1b77757fbee66ef826844b647a8b4c?d=identicon&s=25 Katsuo Isono (katsuo)
on 2008-10-06 04:48
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
This topic is locked and can not be replied to.