search only specific attributes in association queries #136

lironbeni opened this Issue Sep 2, 2012 · 10 comments


None yet
5 participants

Hi Ernie,

How can I restrict the attributes of associated module in association query.

Say I have this in my condition_fields.html.erb file:
<%= a.attribute_select :associations => [:member] %>

It will show in the select box about 24 member fields, while I would like to show only the name and the email of the member.

How to that?


Tried something like this:

ransacker :member_first_name do |parent|
    # joins(:member).select('members.first_name')'members.first_name').first)

    #   Arel::SelectManager.join("genes.member_id =",:members)
    # )

with no luck... getting this error:
TypeError (can't convert Molecular::Gene into String)

Need help... :(

This works fine when using simple search:

def self.member_member_name

But I need it to be added to the advanced search select options, so I'm guessing the only way to do this is by adding a ransacker. (no luck in that end....)

graywh commented Sep 27, 2012

You can restrict the attributes available for searching by overriding the class method ransackable_attributes. To filter attributes out, I call super and remove some, but it might be easier for your to just define the columns explicitly.

def self.ransackable_attributes(auth_object = nil)
  # super - %w( created_at updated_at parent_id )
  %w( name email ) + _ransackers.keys

And if I need to search by association attribute?

For example:
This is my Antibody.rb:

has_one :purchasable, :class_name=>"Catalog::Material", :as => :purchasable, :dependent => :destroy

def self.columns_to_include_in_search
    [{:label => "title", :type => :string},{:label => "description", :type => :string},{:label => "id", :type => :integer},
    {:label => "source", :type => :string}, {:label => "antigene", :type => :string}]

  def self.ransackable_attributes (auth_object = nil) { |c| c[:label]}

And I would like my user to be able to do advance search by the catalog number of this antibody.
So, if I add this:
:label => "purchasable_catalog_number", :type => :string}
to the ``self.columns_to_include_in_searchmethod it will try to search like this:.... where antibodies.purchasable_catalog_number = '15'`
(not good...)

If I change my simple search field to be purchasable_catalog_number_cont it works and do this:
.... where materials.catalog_number = '15'

What am I missing...?
How do I make ransack know when I'm searching in association attribute and not the class attribute..?


alkalinecoffee commented Mar 12, 2014

Any update on this? Also interested in this functionality.


ernie commented Mar 12, 2014



radar commented Mar 12, 2014

@alkalinecoffee You can be sure that if there was an update on this it would be posted in this thread. All the information in this thread is all the information about this problem.

graywh commented Mar 13, 2014

@lironbeni Is defining .ransackable_attributes on the other class not an option?

The way I solved my problem with searching by association fields (purchasable in Antibody for example) is to add them to my columns_to_include_in_search of Antibody this way:

...{:label => :purchasable_catalog_number, :type => :string}, 
{:label => :purchasable_company_id, :type => :purchasableSelect },
{:label => :purchasable_price, :type => :integer} .... 

Please note that it's important to set the label as a symbol and not as a string, because this way ransack "understands" this is an association field.

Regarding the full name search, I'm still struggling.. And now I need to be able to do sorting by association field, that I still not sure how it can be done (will update if I'll manage to do so)


alkalinecoffee commented Mar 17, 2014

@lironbeni thanks for the tip on the symbol vs string. That was the missing piece for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment