I have a problem with STI and relationships in ActiveRecord. I think I missed something in class methods, but I don’t know for sure. Below are my models:
class User < ActiveRecord::Base
has_many :advertisements
end
class Advertisement < ActiveRecord::Base
belongs_to :user
end
class FreeAdvertisement < Advertisement
end
class PaidAdvertisement < Advertisement
end
Now I want to find all FreeAdvertise under a specific user, for example:
u = User.find_by_username('myself')
@freebies = u.free_advertisements.all
It gives an error:
undefined method `free_advertisements' for #<User:0x2360f18>
I can hack it using u.advertisements.find :all, :conditions, but that’s not what I want to do. Please help me solve this problem. Thanks in advance.
source
share