Fabrice Fabrisss
2011-Mar-20 15:30 UTC
ActiveRecord: update a foreign key in an association table
Hello, I''d like to know how to update a foreign key in an association table (many_to_many association) ? Here is my model, Split is the association table between Account and Transaction: class Account < ActiveRecord::Base set_primary_key :guid has_many :splits, :foreign_key => ''account_guid'', :primary_key => ''guid'' has_many :transactions, :through => :splits end class Transaction < ActiveRecord::Base set_primary_key :guid has_many :splits, :foreign_key => ''tx_guid'', :primary_key => ''guid'' has_many :accounts, :through => :splits end class Split < ActiveRecord::Base belongs_to :transaction, :foreign_key => ''tx_guid'', :primary_key => ''guid'' # here is the association that I''d like to change: belongs_to :account, :foreign_key => ''account_guid'', :primary_key => ''guid'' end I''m trying this but it does not work (the value is not updated): account = Account.new transaction.splits.first.account = account # error: prints the old value of account puts transaction.splits.first.account Do you have any idea ? Do I need to create a new Split and delete the old one or is it possible to update the existing one ? Thank you for your help, Fabrice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Mar-20 15:46 UTC
Re: ActiveRecord: update a foreign key in an association table
On 20 March 2011 15:30, Fabrice Fabrisss <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > I''d like to know how to update a foreign key in an association table > (many_to_many association) ? > > Here is my model, Split is the association table between Account and > Transaction: > > class Account < ActiveRecord::Base > set_primary_key :guid > > has_many :splits, :foreign_key => ''account_guid'', :primary_key => > ''guid'' > has_many :transactions, :through => :splits > end > > class Transaction < ActiveRecord::Base > set_primary_key :guid > > has_many :splits, :foreign_key => ''tx_guid'', :primary_key => > ''guid'' > has_many :accounts, :through => :splits > end > > class Split < ActiveRecord::Base > belongs_to :transaction, :foreign_key => ''tx_guid'', :primary_key > => ''guid'' > # here is the association that I''d like to change: > belongs_to :account, :foreign_key => ''account_guid'', :primary_key > => ''guid'' > end > > I''m trying this but it does not work (the value is not updated): > > account = Account.new > transaction.splits.first.account = account > # error: prints the old value of account > puts transaction.splits.first.accountDid you save the split after updating it? transaction.splits.first.account will go back to the database. Try something like split = transaction.splits.first split.account = account split.save! You might need to reload transaction to get it to notice the change. Is this GnuCash by any chance? I have written a read-only rails interface for GnuCash for report generation. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-20 19:39 UTC
Re: ActiveRecord: update a foreign key in an association table
On Mar 20, 3:30 pm, Fabrice Fabrisss <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m trying this but it does not work (the value is not updated): > > account = Account.new > transaction.splits.first.account = account > # error: prints the old value of account > puts transaction.splits.first.account > > Do you have any idea ? Do I need to create a new Split and delete the > old one or is it possible to update the existing one ?If the collection (in this case transaction.splits) isn''t loaded then foo.first (and foo.last) will fetch just that one row from the database. The way it''s implemented, foo.first returns a fresh object each time, so transaction.splits.first.account = account does assign account to that split, however when you call transaction.splits.first.account it''s fetching a fresh copy of that split. if you did foo = transaction.splits.first; foo.account = account then you''d be able to see that the assignment did actually happen Fred> > Thank you for your help, > > Fabrice > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fabrice Fabrisss
2011-Mar-22 16:24 UTC
Re: ActiveRecord: update a foreign key in an association table
Thank you for your advice,> Colin, yes, it''s a GnuCash application which assigns an account to transactionsbased on keywords I solved my problem by defining a primary key on the association table ''Splits'': class Split < ActiveRecord::Base set_primary_key :guid ... end Sincerely, Fabrice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.