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

これは、以下のソリューションのバリエーションを使用して実現できます。

squeelgemをインストールします

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:

あなたのコメントに基づくと、あなたはsqueelgemを必要としないようです。通常の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 に答える