正确的Rails关联设置

时间:2012-06-19 16:48:10

标签: ruby-on-rails ruby-on-rails-3 associations

我设置提醒服务,通过电子邮件发送与人群兴趣和城市相关的交易。基本上,用户输入重要日期(朋友bday,周年纪念日等)以及该特殊人士的兴趣。

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

我应该如何设置交易模型的关联?

到目前为止我有什么......

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

谢谢!

1 个答案:

答案 0 :(得分: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')
相关问题