I’ve got polymorphic association which looks like
class Certificate < ActiveRecord::Base
attr_accessible :certification_id, :certified_id, :certified_type
belongs_to :certification
belongs_to :certified, :polymorphic => true
end
class Certification < ActiveRecord::Base
attr_accessible :name, :slug
belongs_to :association
end
class Association < ActiveRecord::Base
has_many :certifications
attr_readonly :name, :id
attr_protected :name
end
assuming User model
class User < ActiveRecord::Base
has_many :certificates, :as => :certified
end
i am trying to access association object from within polymorphic association
u = User.first
u.certificates returns array of instances of Certificate
u.certificates.first.certification returns instance of Certification
but
u.certificates.first.certification.association returns stack level too deep error and on the second run console/server crashes with illegal hardware instruction message
I do realize that this expression is hardly a queen of code beauty, but it should work, shouldn’t it?
Firstly I think that Association may be an unfortunate choice for a model name.
ActiveRecord::Base already has an instance method called association which you’ve just overridden for your Certification model by setting up an association to a model called Association. I haven’t had a chance to trace exactly what that’s going to do, but I’d guess a method called “association” is probably quite important in reflecting on and operating over a model’s associations and overriding it is going to have exciting consequences.
I’d also suggest that given how awkward the above paragraph is to read that Association maybe isn’t an optimal name for a Rails model anyway!
Try renaming that model and the associations to it to see if it fixes your problem.