0

I need to access an information that is stored 2 relations from the one I'm doing the query on.

class Information < ActiveRecord::Base
  ...
  belongs_to :information_type, polymorphic: true
  ...
end

class InformationTypeOne < ActiveRecord::Base
  ...
  belongs_to :location
  has_one :information, as: :information_type
  ...
end

class InformationTypeTwo < ActiveRecord::Base
  ...
  belongs_to :location
  has_one :information, as: :information_type
  ...
end

class Location < ActiveRecord::Base
   has_many :information_type_ones
   has_many :information_type_twos
end

So what I would like to to do is to find all information belonging to one location, no matter what InformationType it is.

Best case would be something like Information.where(location: 'Location A')

Anyone knows how to realize this?

--- UPDATE ---

I managed to at least get the location belonging to an information by addings this:

delegate :location, to: :information_type

If I now could somehow make this work:

Information.where(location: 1)

I would be very happy. Anyone? :)

Cojones
  • 2,930
  • 4
  • 29
  • 41
  • see this link - it will help you - http://railscasts.com/episodes/154-polymorphic-association – Dipak Panchal Oct 05 '12 at 08:45
  • Unfortunately it doesn't really help me because it doesn't cover my case where I have to go through one model in order to get to the location information – Cojones Oct 08 '12 at 06:55
  • So apparently there's no way to do this properly? I've been googling and searching for a solution for two days now... – Cojones Oct 09 '12 at 03:28

0 Answers0