2014-11-20 1 views
0

그래서 뉴스 레터를 받았지만 전송 된 인용문을 추적하는 방법을 고민 중입니다. 나는 중복을 보내는 것을 피하기 위해 보낸 인용문을 추적하고 싶다.액션 메일러를 통해 매일 고유 한 메시지를 보내는 방법

앱은 매우 간단합니다. 카테고리 모델이 속한 견적 모델과 카테고리 모델에 속한 구독자 모델이 있습니다. 하나의 카테고리에는 구독자와 인용 부호가 있습니다.

아래 코드는 구독자에게 첫 번째 견적을 보냅니다. 물론 매일 새로운 인용문을 보낼 수 있도록 이전에 인용 부호가 가입자에게 보내 졌는지 추적 할 수있는 테이블이나 필드를 유지해야합니다. 나는이 방법은 준비

Category.all.each do |category| 
    category.subscribers.each do |subscriber| 
     SubscriptionMailer.sendmyemail(subscriber.email, category, subscriber.choose_quote(category), category.subscribers).deliver   
    end 
end  

그리고 내 subscriber.rb 모델에서,하지만 내가 원하는 것을 달성하기로 채우기 위해 어떤 코드 확실하지 :

Category.all.each do |category| 
    category.subscribers.each do |subscriber| 
     SubscriptionMailer.sendmyemail(subscriber.email, category, category.quotes.first.body, category.subscribers).deliver   
    end 
end 

추가로 업데이트 할 수 있습니다. 과거에 보낸 내용과 중복되지 않고 고유 한 하루에 한 번 인용자를 구독자에게 보낼 수 있습니까? 아니면 더 쉬운 방법은 주문을 무작위로 추출하는 것입니다. 가능한가요?

def choose_quote(cat) 
    # send quote which is unique 
    # Meaning which was not sent 
end 
+0

각 견적을 보낸 후 견적을 사용자에게 보내면 별도의 테이블에 기록을 저장할 수 있습니다. – Rubyrider

+0

예를 들어 보겠습니다. ty –

+0

물론입니다. 내가 대답을 써 보자. – Rubyrider

답변

7
  • 당신은 당신의 가입자 당신은 사용자에게 전송 된 인용를 추적 할 필요가
  • 에 따옴표를 보내고있다.

내가 언급 한 특정 문제에 대한 가능한 솔루션 디자인을 제안합니다.

rails generate model quote_history subscriber:references quote:references # assumed there 
#is a model subscriber and quote 

이제 subscriber_quote_history는 가입자와 따옴표에도 속할 것입니다.

class QuoteHistory < ActiveRecord::Base 
    belongs_to :subscriber 
    belongs_to :quote 
    validates :quote_id, :uniqueness => {scope: :subscriber_id} 
end 

따라서 구독자, 견적 모두에는 quote_histories가 보내 져야합니다.

class Subscriber < ActiveRecord::Base 
    has_many :quote_histories 
    ..... 
end 

class Quote < ActiveRecord::Base 
    has_many :quote_histories 
    scope :uniq_quote_for, Proc.new {|subscriber_id| includes(:quote_histories).where.not(quote_history: {subscriber_id: subscriber_id})} 
    ..... 
end 

지금은 원하는 방식으로 어떤 조건을 작성하는 방법을 제안 :이 당신이 설계하는 데 도움이 될 것입니다 생각

Category.all.each do |category| 
    category.subscribers.each do |subscriber| 
     quote = subscriber.choose_quote(category) 
     if quote # if quote found then send email 
     SubscriptionMailer.sendmyemail(subscriber.email, category, quote, category.subscribers).deliver  
     else 
     next # if no quote found then go to next loop. 
     end  
    end 
end 

: 당신의 우편물에서

class Subscriber < ActiveRecord::Base 
def choose_quote(cat) 
    quote = cat.quotes.uniq_quote_for(self.id).order("RANDOM()").first #Add a new history after that for that. here you will decide your own business logic. 
    return quote if self.quote_histories.create(qoute_id: quote.id) 
    return nil 
end 
end 

가 비슷한 할 귀하의 솔루션. 여기에서 필자는 시각화와 내 논리에서 모든 것을 썼다. 원하지 않는 기존의 오류 또는 그 밖의 것을 건너 뛰십시오. 내 의도는 여기에 문제를 정렬하는 방법을 보여주는 것입니다.

감사합니다.

+0

감사합니다. 이것은 대단합니다. 마지막 줄에있는 find 메소드에 무엇이 들어갈 지 지정할 수 있습니까? 예 : Quote.uniq_quote_for (self.id) .find (....) 그리고 나는 당신의 대답을 받아 들일 것입니다. –

+0

비즈니스 논리에 따라 다릅니다. 메일 하나당 하나의 견적을 보내시겠습니까? – Rubyrider

+0

또한 견적과 카테고리 사이의 연관성을 알려주십시오. – Rubyrider