Hi All, I am not able to update the association''s object attributes along with my model object. For example, my models are, class Library has_many :books end class Book belongs_to :library end I am getting the attribute values for both the models from the view and I try to update both in a single transaction. When I tried like, @book.attributes = params[:book] @book.library.attributes = params[:library] @book.save It is updating the books table but not the libraries table. But when I try the same logic for new book and new library creation, it works fine. The problem is only when updating. Now i have implemented as, @book.update_attributes(params[:book]) @book.update_attributes(params[:library]) But I want to update both the attributes in a single statement and in a single transaction. Any suggestions??? Thanks in advance... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
afaik you can''t do anything like that. Also it should be: book.library.update_attributes(params[:library]) On Dec 28, 2007 3:35 PM, Karthi kn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi All, > > I am not able to update the association''s object attributes along with > my model object. > > For example, my models are, > > class Library > has_many :books > end > > class Book > belongs_to :library > end > > I am getting the attribute values for both the models from the view and > I try to update both in a single transaction. When I tried like, > > @book.attributes = params[:book] > @book.library.attributes = params[:library] > @book.save > > It is updating the books table but not the libraries table. > But when I try the same logic for new book and new library creation, it > works fine. The problem is only when updating. > > Now i have implemented as, > > @book.update_attributes(params[:book]) > @book.update_attributes(params[:library]) > > But I want to update both the attributes in a single statement and in a > single transaction. > > Any suggestions??? > > Thanks in advance... > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> afaik you can''t do anything like that. > > Also it should be: > > book.library.update_attributes(params[:library]) >Yes. Sorry. That was my typing mistake. But, is it not possible to update both in a single transaction? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Like I said, no there isn''t as far as I know. On Dec 28, 2007 3:50 PM, Karthi kn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ryan Bigg wrote: > > afaik you can''t do anything like that. > > > > Also it should be: > > > > book.library.update_attributes(params[:library]) > > > > > Yes. Sorry. That was my typing mistake. But, is it not possible to > update both in a single transaction? > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2007-Dec-28 17:03 UTC
Re: Problem in updating the association object attributes
On Dec 28, 2007 12:20 AM, Karthi kn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ryan Bigg wrote: > > afaik you can''t do anything like that. > > > > Also it should be: > > > > book.library.update_attributes(params[:library]) > > > > > Yes. Sorry. That was my typing mistake. But, is it not possible to > update both in a single transaction?Assuming that the tables are in the same database, you can do it in a single transaction, albeit not in a single save. Library.transaction do @library.update_attributes(params[:book]) @book.update_attributes(params[:library]) end Whether you use Library or Book as the receiver of the transaction method shouldn''t matter since the transaction applies to the database connection which is shared assuming, as I said, that both tables are in the same DB. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---