ruby on rails - How to search for the fields of the parent model through child model in polymorphic association -
class book < activerecord::base belongs_to :owner, polymorphic: true end class user < active record::base has_many :books, as: :owner end
now book has 3 fields - name, owner_id , owner_type
. need search name of user through books.
something ..
book.includes(:user).where("user_name ?","bla")
can me this?
if this:
book.includes(:owner)
can not eagerly load polymorphic association
you error above.
you need define relationship between book , user
class book < activerecord::base belongs_to :owner, polymorphic: true belongs_to :user, foreign_key: 'owner_id', conditions: "owner_type = 'user'" end
see here
Comments
Post a Comment