简体   繁体   English

Rails ActiveRecord查询(交叉模型)

[英]Rails ActiveRecord Query (Cross model)

I have an app that lets users input dates & interests that relate to those dates .I need to send them deals (a few days before the date - Via Email) that are based off of their interests and location. 我有一个应用程序,可以让用户输入与这些日期相关的日期和兴趣。我需要根据他们的兴趣和位置向他们发送交易(在日期前几天-通过电子邮件)。 I have all the models setup and recording the data properly, just wondering how to query the models for the dates and then send the appropriate deal based off of the city and interests. 我已经设置了所有模型并正确记录了数据,只是想知道如何查询模型的日期,然后根据城市和兴趣来发送适当的交易。

Notes: 笔记:

*Each city and interest category has only 1 deal *每个城市和兴趣类别只有1笔交易

*I have several different models for types of dates (Holidays, Occasions, Friends Birthdays ect).. all are pretty much identical in structure. *我有几种不同的日期类型模型(假期,场合,朋友生日等)。所有结构都差不多。

*All interests for each type of date are stored in person_interests. *每种日期类型的所有兴趣都存储在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

You can achieve this using a variation of the solution below: 您可以使用以下解决方案的变体来实现此目的:

Install the squeel gem 安装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

Now, user.deals returns the deals that will be active in next 3 days matching the user's city OR interests. 现在, user.deals返回将在接下来的3天内与用户所在城市或兴趣相匹配的交易。

Edit 1: 编辑1:

Based on your comment it looks like you don't need the squeel gem. 根据您的评论,看来您不需要Squeel gem。 You can achieve what you want using regular AR syntax. 您可以使用常规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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM