Ransack: Polymorphic Association Issue. Does ransack support it?

Created on 29 Sep 2014  路  4Comments  路  Source: activerecord-hackery/ransack

This is my code:

class Comment
  belongs_to :commentable, polymorphic: true
end

class Video
  has_many :comments, as: :commentable
end

class Image
  has_many :comments, as: commentable
end

What I am doing:

Comment.search(commentable_title_eq: 'some title').result.to_sql

What I am getting:

NameError: uninitialized constant Comment::Commentable

If I change my code to:

class Comment
  belongs_to :commentable, polymorphic: true, class_name: 'Video'
end

It works, but I am breaking the polymorphic association.

Can anyone help please?

Thanks

Most helpful comment

Please try: commentable_of_Video_type_title_eq, see the wiki for details on Polymorphic Searches.

All 4 comments

Please try: commentable_of_Video_type_title_eq, see the wiki for details on Polymorphic Searches.

How would I know which type to search for when writing search form?

@avit I have the same scenario but I not sure how deal with sorting. Kindly assist me.
I am getting Polymorphic associations do not support computing the class.

when I try something in view
= sort_link(@search, :licensee_of_Device_type_quantity, {} , {remote: true})

Please try: commentable_of_Video_type_title_eq, see the wiki for details on Polymorphic Searches.

What if the type you want to reference is namespaced? :cry:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mbajur picture mbajur  路  5Comments

ivanovaleksey picture ivanovaleksey  路  3Comments

AnnaErshova picture AnnaErshova  路  3Comments

zenati picture zenati  路  4Comments

seanfcarroll picture seanfcarroll  路  4Comments