I have a couple of models that are composites of multiple objects. I basically manage them manually for saves and updates. However, when I select items out, I don’t have access to the associated properties of said item. For example:
class ObjectConnection < ActiveRecord::Base
def self.get_three_by_location_id location_id
l=ObjectConnection.find_all_by_location_id(location_id).first(3)
r=[]
l.each_with_index do |value, key|
value[:engine_item]=Item.find(value.engine_id)
value[:chassis_item]=Item.find(value.chassis_id)
r << value
end
return r
end
end
and each item:
class Item < ActiveRecord::Base
has_many :assets, :as => :assetable, :dependent => :destroy
When I use the ObjectLocation.find_three_by_location_id, I don’t have access to assets whereas if use Item.find(id) in most other situations, I do.
I tried using includes but that didn’t seem to do it.
thx
Sounds like the simplest solution would be to add methods to your
ObjectConnectionmodel for easy access like so:I’m not exactly sure what you’re asking… If this doesn’t answer what you’re asking, then can you try to be a little bit more clear with what exactly you are trying to accomplish? Are the
ChassisandEnginemdoels supposed to be polymorphic associations with yourItemmodel?Also, the code you’re using above won’t work due to the fact that you are trying to dynamically set properties on a model. It’s not your calls to
Item.findthat are failing, it’s your calls tovalue[:engine_item]=andvalue[:chassis_item]that are failing. You would need to modify it to be something like this if you wanted to keep that flow:But I still think that this whole method seems unnecessary due to the fact that if you setup proper associations on your
ObjectConnectionmodel to begin with, then you don’t need to go and try to handle the associations manually like you’re attempting to do here.