我需要将winning_numbers 表中的一行7 个属性与另一个表lottery_selections 中的1 行或更多行进行比较,该表在rails 3.2.8 中包含6 个属性。从结果中,我想计算一个表中与另一个表匹配的属性数量并呈现结果。
我有两个模型:lottery_selections 和winning_numbers
# == Schema Information
#
# Table name: lottery_selections
#
# id :integer not null, primary key
# syndicate_id :integer
# lottery_selection_1 :integer
# lottery_selection_2 :integer
# lottery_selection_3 :integer
# lottery_selection_4 :integer
# lottery_selection_5 :integer
# lottery_selection_6 :integer
# lottery_selection_date :datetime
# created_at :datetime not null
# updated_at :datetime not null
#
class LotterySelection < ActiveRecord::Base
attr_accessible :lottery_selection_1, :lottery_selection_2, :lottery_selection_3, :lottery_selection_4,\
:lottery_selection_5, :lottery_selection_6, :lottery_selection_date, :syndicate_id
belongs_to :winning_number
#validates_associated :winning_number
validates :lottery_selection_1, :lottery_selection_2, :lottery_selection_3, :lottery_selection_4,\
:lottery_selection_5, :lottery_selection_6, :presence => true, :numericality => {:greater_than_or_equal_to => 1}, \
:numericality => {:less_than_or_equal_to => 49}
UNIQU_FIELDS = [:lottery_selection_1, :lottery_selection_2, :lottery_selection_3, :lottery_selection_4,\
:lottery_selection_5, :lottery_selection_6]
validate :lottery_numbers_are_unique
def lottery_numbers_are_unique
unless UNIQU_FIELDS.map{|field| self[field] }.uniq.length == UNIQU_FIELDS.length
errors[:base] << "You have repeated one or more numbers for that day's draw"
end
end
end
# == Schema Information
#
# Table name: winning_numbers
#
# id :integer not null, primary key
# winning_numbers_date :datetime
# winning_number_1 :integer
# winning_number_2 :integer
# winning_number_3 :integer
# winning_number_4 :integer
# winning_number_5 :integer
# winning_number_6 :integer
# winning_number_bonus :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class WinningNumber < ActiveRecord::Base
attr_accessible :winning_number_1, :winning_number_2, :winning_number_3, :winning_number_4, :winning_number_5,\
:winning_number_6, :winning_number_bonus, :winning_numbers_date
has_many :lottery_selections
#validates_associated :lottery_selections
validates :winning_number_1, :winning_number_2, :winning_number_3, :winning_number_4, :winning_number_5,\
:winning_number_6, :winning_number_bonus, :presence => true, :numericality => {:greater_than_or_equal_to => 1}, \
:numericality => {:less_than_or_equal_to => 49}
#Below checks that for any given date there can only be one set of winning numbers
validates :winning_numbers_date, uniqueness: { scope: [:winning_number_1, :winning_number_2, :winning_number_3,\
:winning_number_4, :winning_number_5, :winning_number_6, :winning_number_bonus] }
UNIQ_FIELDS = [:winning_number_1, :winning_number_2, :winning_number_3, :winning_number_4, :winning_number_5, :winning_number_6, :winning_number_bonus]
validate :winning_numbers_are_unique
def winning_numbers_are_unique
unless UNIQ_FIELDS.map{|field| self[field] }.uniq.length == UNIQ_FIELDS.length
errors[:base] << "You have repeated one or more numbers for that day's draw"
end
end
end
您能否建议我如何进行比较并使用结果来呈现结果?