在 Rails 用户模型中设置 default_address

发布于 10-06 08:57 字数 695 浏览 3 评论 0原文

我目前正在为 has_many :addresses; 的 User 开发一个模型。这些地址形成一个小型地址簿,用户可以从中选择送货和邮寄地址。

我的问题是,将 has_many 关系中的 Address 对象之一标记为默认地址的最佳方法是什么?

一种方法(我认为我已经做到了这一点)是维持这种关系:

class User < ActiveRecord::Base
  has_many :addresses, :dependent => :destroy
  has_one :default_address, :class_name => "Address"

class Address < ActiveRecord::Base
  belongs_to :user
  belongs_to :user, :foreign_key :default_address_id

但我知道这很草率,因为我的默认地址和我的用户之间将有两个链接。

我应该在地址中设置默认布尔值,并使用 scope 获取默认值吗? (这增加了额外的验证,因为我不想要多个默认地址......嗯。)

我认为这在具有地址管理的应用程序中一定经常出现,所以我想我会列出我的想法并询问。任何有关最佳实践的建议将不胜感激。

I'm currently working on a model for a User that has_many :addresses; These addresses form a small address book from which a user can choose shipping and mailing addresses.

My question is, what's the the best way for me to tag one of the Address objects in the has_many relationship as the default address?

One way (I think I've got this right) would be to maintain this relationship:

class User < ActiveRecord::Base
  has_many :addresses, :dependent => :destroy
  has_one :default_address, :class_name => "Address"

class Address < ActiveRecord::Base
  belongs_to :user
  belongs_to :user, :foreign_key :default_address_id

but I know that's sloppy, as I'll have two links between my default address and my user.

Should I be setting a default boolean in address, and grabbing the default using scope? (This adds an extra validation, as I don't want multiple default addresses... hmm.)

I figure this must come up fairly often in applications with address management, so figured I'd lay out my ideas and just ask. Any recommendations on best practices would be much appreciated.

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

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

发布评论

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

评论(2

通知家属抬走2024-10-13 08:57:34

我会在地址中添加一个“is_default”标志。然后,您需要在地址中进行验证,如下所示:

validates_uniqueness_of :is_default, :scope => :user_id, :unless => Proc.new { |address| address.is_default == 0 }

这将确保您只获得一个默认地址,但将允许您想要的任意数量的非默认地址。

I'd add a 'is_default' flag to Addresses. Then you'll need a validation in the Addresses that is something like:

validates_uniqueness_of :is_default, :scope => :user_id, :unless => Proc.new { |address| address.is_default == 0 }

This will ensure you only get one default address but will allow for as many non-default addresses as you want.

╭⌒浅淡时光〆2024-10-13 08:57:34

如果是我,我会说外键属于用户,在地址上使用布尔方法来确定它是否是默认地址(假设您在应用程序中需要它):

class User < ActiveRecord::Base
  has_many :addresses, :dependent => :destroy
  belongs_to :default_address, :class_name => "Address"
end

class Address < ActiveRecord::Base
  belongs_to :user
  def default_address?
    user.default_address == self
  end
end

另一个潜在的途径是给出地址是一个布尔标志,表示它是默认地址,并使用用户模型中的访问器来访问它:

class User
  has_many :addresses
  def default_address
    addresses.select(&:default_address?) #overly simple implementation
  end
end

您的应用程序将需要处理逻辑以确保使用第二种安排只有一个默认地址。

If it was me, I'd say the foreign key belongs on the User, with a boolean method on the address to figure out if its the default address (assuming you'll need that in your app):

class User < ActiveRecord::Base
  has_many :addresses, :dependent => :destroy
  belongs_to :default_address, :class_name => "Address"
end

class Address < ActiveRecord::Base
  belongs_to :user
  def default_address?
    user.default_address == self
  end
end

Another potential avenue is to give the address a boolean flag to indicate that its a default address and use an accessor in the User model to get to it:

class User
  has_many :addresses
  def default_address
    addresses.select(&:default_address?) #overly simple implementation
  end
end

Your app will need to handle the logic to ensure there's only one default address using the second arrangement.

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