Rails has_many :though STI

喜夏-厌秋 提交于 2019-12-23 04:01:41

问题


Lets start with code that may go like this:

class User < ActiveRecord::Base
  has_many :photos
  has_many :submitted_photos
  has_many :posted_photos

  has_many :handshakes, through: :photos
end

class Photo < ActiveRecord::Base
  belongs_to :user
end

class PostedPhoto < Photo
  has_many :handshakes, inverse_of: :poster, foreign_key: 'poster_id'
end

class SubmittedPhoto < Photo
  has_many :handshakes, inverse_of: :submitter, foreign_key: 'submitter_id'
end

class Handshake < ActiveRecord::Base
  belongs_to :submitter, class_name: 'SubmittedPhoto'
  belongs_to :poster,    class_name: 'PostedPhoto'
end

The STI part and associations between photos and handshakes work fine. The problem is getting all user's handshakes through his photos.

Using the code above Rails will obviously complain that model Photo does not have an association called handshakes. Is there any facility in AR that would allow me to specify a relation of this kind? If not, what query would you write to get them?


回答1:


You could use in User:

has_many :submitted_handshakes, through: :submitted_photos, source: :handshakes
has_many :posted_handshakes, through: :posted_photos, source: :handshakes

I understand this works as is you had:

user.submitted_handshakes ----> user.submitted_photos.handshakes

Obviously handshakes method does not exist in submitted_photos, but AR made this for you by joining tables.




回答2:


I ended up using the following code, looked like an easiest option to solve this problem.

class User
  def handshakes
    Handshake.where('submitter_id IN (?) OR poster_id IN (?)', submitted_photo_ids, posted_photo_ids)
  end
end


来源:https://stackoverflow.com/questions/20245705/rails-has-many-though-sti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!