Rails 嵌套属性:需要至少两条记录

发布于 2024-10-05 10:44:22 字数 597 浏览 1 评论 0原文

如何才能使提交产品时至少需要两条选项记录?

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :options, :dependent => :destroy
  accepts_nested_attributes_for :options, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
  validates_presence_of :user_id, :created_at
  validates :description, :presence => true, :length => {:minimum => 0, :maximum => 500}
end

class Option < ActiveRecord::Base
  belongs_to :product
  validates :name, :length => {:minimum => 0, :maximum => 60}                  
end

How can I make it so that at least two option records are required to submit a product?

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :options, :dependent => :destroy
  accepts_nested_attributes_for :options, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
  validates_presence_of :user_id, :created_at
  validates :description, :presence => true, :length => {:minimum => 0, :maximum => 500}
end

class Option < ActiveRecord::Base
  belongs_to :product
  validates :name, :length => {:minimum => 0, :maximum => 60}                  
end

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

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

发布评论

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

评论(5

恍梦境° 2024-10-12 10:44:22
class Product < ActiveRecord::Base
  #... all your other stuff
  validate :require_two_options

  private
    def require_two_options
      errors.add(:base, "You must provide at least two options") if options.size < 2
    end
end
class Product < ActiveRecord::Base
  #... all your other stuff
  validate :require_two_options

  private
    def require_two_options
      errors.add(:base, "You must provide at least two options") if options.size < 2
    end
end
盛夏已如深秋| 2024-10-12 10:44:22

只是考虑一下 karmajunkie 答案:我会使用 size 而不是 count 因为如果某些构建(且未保存)的嵌套对象有错误,则不会考虑它(它不是尚未在数据库上)。

class Product < ActiveRecord::Base
  #... all your other stuff
  validate :require_two_options

  private
    def require_two_options
      errors.add(:base, "You must provide at least two options") if options.size < 2
    end
end

Just a consideration about karmajunkie answer: I would use size instead of count because if some built (and not saved) nested object has errors, it would not be considered (its not on database yet).

class Product < ActiveRecord::Base
  #... all your other stuff
  validate :require_two_options

  private
    def require_two_options
      errors.add(:base, "You must provide at least two options") if options.size < 2
    end
end
负佳期 2024-10-12 10:44:22

如果您的表单允许删除记录,则 .size 将不起作用,因为它包含标记为销毁的记录。

我的解决方案是:

validate :require_two_options

private
 def require_two_options
    i = 0
    product_options.each do |option|
      i += 1 unless option.marked_for_destruction?
    end
    errors.add(:base, "You must provide at least two option") if i < 2
 end

If your form allows records to be deleted then .size will not work as it includes the records marked for destruction.

My solution was:

validate :require_two_options

private
 def require_two_options
    i = 0
    product_options.each do |option|
      i += 1 unless option.marked_for_destruction?
    end
    errors.add(:base, "You must provide at least two option") if i < 2
 end
病毒体 2024-10-12 10:44:22

更整洁的代码,用 Rails 5 测试:

class Product < ActiveRecord::Base
  OPTIONS_SIZE_MIN = 2
  validate :require_two_options

  private

  def options_count_valid?
    options.reject(&:marked_for_destruction?).size >= OPTIONS_SIZE_MIN
  end

  def require_two_options
    errors.add(:base, 'You must provide at least two options') unless options_count_valid?
  end
end

Tidier code, tested with Rails 5:

class Product < ActiveRecord::Base
  OPTIONS_SIZE_MIN = 2
  validate :require_two_options

  private

  def options_count_valid?
    options.reject(&:marked_for_destruction?).size >= OPTIONS_SIZE_MIN
  end

  def require_two_options
    errors.add(:base, 'You must provide at least two options') unless options_count_valid?
  end
end
草莓味的萝莉 2024-10-12 10:44:22

我想知道为什么没有人提到这个简单的解决方案。只需将其添加到您的父类中:

class Product < ActiveRecord::Base

  ...
  
  validates :options, :length => { :minimum => 2 }

end

在 Rails 5.2.8(也可能更高版本)中对我来说就像一个魅力。

I wonder why nobody mentioned this simple solution. Just add this to your parent class:

class Product < ActiveRecord::Base

  ...
  
  validates :options, :length => { :minimum => 2 }

end

Works like a charm to me in Rails 5.2.8 (and probably above as well).

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