I have the following:
class Item < ActiveRecord::Base
# assets
has_many :assets, :as => :assetable, :dependent => :destroy
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
And would like to be able to do:
a=Asset.find(5)
a.item # no dice
How would I get the associated item starting with the asset?
thx
In order to get the associated item, you need to use the relationship name that you setup in the Asset class. Since you have declared this relationship as
:assetable, you need to refer to the item as “assetable”.Assuming you have your database setup correctly according to the Rails guide, you should be able to do the following: