如何比较Rails 3.2.8中两个不同表的两行

时间:2012-12-15 11:06:23

标签: ruby-on-rails

我需要将wins_numbers表中的7行属性与另一个表中的1行或多行进行比较,lottery_selections,包含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

您能告诉我如何进行比较并使用结果来呈现结果吗?

1 个答案:

答案 0 :(得分:0)

最有效的解决方案(更多的rails开发人员建议不要这样做):在数据库上创建一个存储过程,您可以在其中进行比较并返回结果。

你可以做的事情很快(虽然你研究得越多越好,在彩票应用上你可能会有很多门票而你需要运行一些非常有效的东西):

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 (...")

依此类推,它可能不是最好的算法,但它可以运行并使用数据库引擎来做同情。

但是对于这样的问题,我只需聘请一位DBA顾问为我编写存储过程。