0

我有一个应用程序,可让用户输入与这些日期相关的日期和兴趣。我需要根据他们的兴趣和位置向他们发送交易(日期前几天 - 通过电子邮件)。我已经正确设置了所有模型并记录了数据,只是想知道如何查询模型的日期,然后根据城市和兴趣发送适当的交易。

笔记:

*每个城市和兴趣类别只有1个交易

*对于日期类型(假期、场合、朋友生日等),我有几种不同的模型。所有的结构都几乎相同。

*每种日期的所有兴趣都存储在 person_interests 中。

    Models:

    Class User
      belongs_to :province
      belongs_to :city
      has_many :friends_bdays
      has_many :occasions
      has_many :person_interests, :as => :person
      has_many :interests, :through => :person_interests
      has_many :user_holidays
      has_many :holidays, :through => :user_holidays
      has_many :anniversaries
    end

    class Deal < ActiveRecord::Base
       belongs_to :interest
       belongs_to :city
       belongs_to :store  
    end

   class Store < ActiveRecord::Base
      has_many :deals
      belongs_to :city
      belongs_to :province
    end

    class PersonInterest < ActiveRecord::Base
      belongs_to :interest
      belongs_to :person, :polymorphic => true  
    end

    class Interest < ActiveRecord::Base
      has_many :person_interests
      has_many :deals
    end


    class Occasion < ActiveRecord::Base
      belongs_to :user
      belongs_to :admin_user
      has_many :person_interests, :as => :person
      has_many :interests, :through => :person_interests    
    end


    class Anniversary < ActiveRecord::Base
      belongs_to :user
      has_many :person_interests, :as => :person
      has_many :interests, :through => :person_interests
    end


  class Friend_bday < ActiveRecord::Base
    belongs_to :user
    has_many :person_interests, :as => :person
    has_many :interests, :through => :person_interests
  end
4

1 回答 1

1

您可以使用以下解决方案的变体来实现此目的:

安装squeel gem

class User
  def deals(reload=false)
    @deals = nil if 
    @deals ||= Deal.where{ 
     ( (:city => city_id) | ( :interest_id => interest_ids) ) & 
     :deal_date => (Time.now..3.days.from_now) 
    }
  end
end

现在,user.deals返回与用户的城市或兴趣相匹配的未来 3 天内有效的交易。

编辑1:

根据您的评论,您似乎不需要 squeel gem。您可以使用常规 AR 语法来实现您想要的。

class User
  def deals(reload=false)
    @deals = nil if reload

    @deals ||= Deal.where(
      :city => city_id, 
      :interest_id => interest_ids, 
      :deal_date => (Time.now..3.days.from_now) 
    )
  end
end
于 2012-06-26T19:25:24.793 回答