On 20 March 2011 15:30, Fabrice Fabrisss <[email protected]> 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.account
Did 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

