1

我需要将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

您能否建议我如何进行比较并使用结果来呈现结果?

4

1 回答 1

0

Most effective solution (even more rails developer will advise not to do it): Create a stored procedure on the database where you can do the comparison and return the result.

Something quick you can do (although the more you research the better, on a lottery app you may have a lot of tickets and you need to run something that is really effective):

winning_numbers = # Do a query and put the array of winning numbers here

seven_numbers_match = LotterySelection.where("number_1 in (?) and number_2 in (?) and number_3 in (?) and number_4 in (?) and number_5 in (?) and number_6 in (?) and number_7 in (?)", winning_numbers, winning_numbers, winning_numbers, winning_numbers, winning_numbers, winning_numbers, winning_numbers)

six_number_match = LotterySelection.where("(number_1 in (?) and number_2 in (?) and number_3 in (?) and number_4 in (?) and number_5 in (?) and number_6 in (?) and number_7 not in(?)") OR (number_1 in (?) and number_2 in (?) and number_3 in (?) and number_4 in (?) and number_5 in (?) and number_6 not in (?) and number_7 in (?)) OR (...")

And so on, it may not be the best algorithm but it works and use the db engine to do the compassion.

But definitely for a problem like this, I'll simply hire a DBA consultant to write the stored procedure for me.

于 2012-12-15T11:21:34.470 回答