我有帐户和帐户地址。一个帐户可以有多个 AccountAddressess,我想指定一个作为“default_account_address”,因此在 Account 表中,我有一个名为“default_account_address_id”的列。这是模型的当前状态。
class Account < ActiveRecord::Base
has_many :account_addresses
belongs_to :default_account_address,
:class_name => "AccountAddress",
:inverse_of => :account_assigned_to_as_default
end
class AccountAddress < ActiveRecord::Base
belongs_to :accounts
has_one :account_assigned_to_as_default,
:class_name => "Account",
:foreign_key => :default_account_address_id,
:inverse_of => :default_account_address
end
除了 @account.default_account_address 返回一个帐户地址和 @account.account_addresses 返回一个空数组之外,这很好用。
所以,问题是@account.account_addresses 中不包含默认帐户地址。
有关解决此问题的最佳方法的任何想法?我考虑过habtm,但似乎不合适。我考虑使用 has_one :default_account_address,但这没有意义,因为 default_account_address_id 列在帐户表上。谢谢。