2011-02-05 15 views
0

私のモデルで私を助け:スコープと条件

Subscriber 
has_and_belongs_to_many :skills 

Skill 
has_many :positions 

Position 
belongs_to :skill 

私は
2が加入者の後に作成された最後の日中に作成されている...
1.ポジションを持っているすべての加入者を見つけたいです

まず条件を作成しました(subscriber.rb)簡単です:

scope :notify_today, 
includes(:skills => :positions).where('positions.created_at > ?', 1.day.ago) 
ラムダを使用するための第二の条件 it was suggestedについては

が、私はかなりそれを取得できませんでした - 私は初心者だ...

私は電子メール通知を送信するためにcron.rakeでファイルをスコープを使用していますincludesは熱心ローディングに関連するレコードのためのものであるjoinsは、関連レコードに検索するためのものであることを

Subscriber.notify_today.each {|subsc| UserMailer.delay.new_positions_mail(subsc)} 

答えて

1
scope :notify_today, lambda { 
    joins(:skills => :positions). 
    where('positions.created_at > subscribers.created_at'). 
    where('positions.created_at > ?', 1.day.ago) 
} 

注:サブスクライバへ。

+0

素晴らしい! :)これはとても簡単です。ありがとうございました。 "曖昧な列名"というメッセージがあったので、 "created_at"を "subscribers.created_at"に変更するだけでした。 –

+0

フィードバックいただきありがとうございます。他の誰かが同じ種類の質問をしている場合は、私の答えにあなたの修正を加えました。 – yfeldblum