在 Rails 中为 has_many 关系指定默认记录的最佳方法

发布于 2024-10-30 23:43:49 字数 947 浏览 0 评论 0原文

我有帐户和帐户地址。一个帐户可以有多个 AccountAddresses,我想指定一个作为“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 列位于帐户表上。谢谢。

I have Accounts and AccountAddressess. An account can have many AccountAddressess and I would like to specify one as the "default_account_address", so in the Account table, I have a column named "default_account_address_id". Here is the current state of the models.

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

This works fine except for the fact that @account.default_account_address returns an account address and @account.account_addresses returns an empty array.

So, the issue is that the default account address is not included in @account.account_addresses.

Any ideas on the best way to approach this issue? I considered habtm, but it doesn't seem appropriate. I considered using has_one :default_account_address, but this doesn't make sense because the default_account_address_id column is on the account table. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冧九 2024-11-06 23:43:49

可能有更好的方法,但我想到的是:

class Account < ActiveRecord::Base
  has_many :account_addresses

  def default_address
    account_addresses.find_by_default true
  end
end

class AccountAddress < ActiveRecord::Base
  belongs_to :accounts
end

这当然假设您在 AccountAddress 中有一个名为 default 的布尔列。我可能会向 AccountAddress 添加验证,以检查是否只有 1 个 AccountAddress 被标记为给定 account_id 的默认值。您还可以在 AccountAddress 中创建一个方法,该方法不仅将地址标记为默认地址,而且还为您取消标记所有关联的地址。

就像我说的,可能有更好的东西,但这应该允许默认地址也显示在@account.account_addresses中。

There is probably a better way, but here is something that came to mind:

class Account < ActiveRecord::Base
  has_many :account_addresses

  def default_address
    account_addresses.find_by_default true
  end
end

class AccountAddress < ActiveRecord::Base
  belongs_to :accounts
end

This of course assumes you have a boolean column named default in AccountAddress. I would probably add validation to AccountAddress that would check that there is only 1 AccountAddress marked as default for a given account_id. You could also create a method in AccountAddress that not only marks an address as default, but also unmarks all associated addresses for you.

Like I said, there is probably something better out there, but this should allow the default address to also show in @account.account_addresses.

挽袖吟 2024-11-06 23:43:49

另一个解决方案:

class Account < ActiveRecord::Base
  has_many :account_addresses
  has_one :default_account_address, -> { find_by_default true },
          class_name: 'AccountAddress'
end

class AccountAddress < ActiveRecord::Base
  belongs_to :accounts
end

Another solution:

class Account < ActiveRecord::Base
  has_many :account_addresses
  has_one :default_account_address, -> { find_by_default true },
          class_name: 'AccountAddress'
end

class AccountAddress < ActiveRecord::Base
  belongs_to :accounts
end
画中仙 2024-11-06 23:43:49

我不知道这是否是最佳实践,但是,我只需向表中添加一个属性“mainaddress”,并在关系中仅使用 has_many/belongs_to 。在帐户模型中,我将放置一个函数,该函数使用一个简单的查询来获取主地址,其中 mainaddress 为 true。

I don't know if this is best practice, however, I would just add an attribute "mainaddress" to the table and use just has_many/belongs_to in the relations. Into the account model I would put a function that fetches the main address using a simple query where mainaddress is true.

如梦亦如幻 2024-11-06 23:43:49

另一个想法是使用 https://github.com/swanandp/acts_as_list ,然后将默认地址视为那么,将某个地址设置为默认地址就很简单:

address.move_to_top if params[:set_as_default]

特别是如果您需要的只是将其显示在某个列表或组合框中的第一个地址。
查询默认地址也很容易,因为它始终位于“位置”范围内的第一个。

Another idea is to use https://github.com/swanandp/acts_as_list and then treat default address as the top one, then, setting some address as default would be as simple as:

address.move_to_top if params[:set_as_default]

Especially if all you need is to show it the first in some list or combo box.
Querying default address is also easy, since it always first in the scope of "position".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文