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