0

我设置了一个提醒服务,通过电子邮件发送与个人兴趣和城市相关的交易。基本上,用户输入重要日期(朋友生日、周年纪念日等)和那个特别的人的兴趣。

我想根据 1)用户所在城市和 2)相关人员的兴趣向他们发送交易

我应该如何为 Deal 模型设置关联?

到目前为止我有什么..

class User < ActiveRecord::Base
belongs_to :city
has_many :person_interests, :as => :person
has_many :interests, :through => :person_interests

end

class City < ActiveRecord::Base
 attr_accessible :name 
  belongs_to :province
  has_many :users
end

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

class Interest < ActiveRecord::Base
  has_many :person_interests
end

谢谢!

4

1 回答 1

1

如果一笔交易可能适用于多个利益,您可以从以下内容开始:

class Deal < ActiveRecord::Base 
  belongs_to :interests
  belongs_to :city
end

class City < ActiveRecord::Base
 attr_accessible :name 
  belongs_to :province
  has_many :users
  has_many :deals
end

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

然后你可以做类似的事情

@relevant_deals = @city.deals.where(:interest_id => 'abc')

或者

@relevant_deals = @interest.deals.where(:city_id => 'def')
于 2012-06-19T18:45:08.640 回答