Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search only specific attributes in association queries #136

Closed
lironbeni opened this issue Sep 2, 2012 · 11 comments
Closed

search only specific attributes in association queries #136

lironbeni opened this issue Sep 2, 2012 · 11 comments

Comments

@lironbeni
Copy link

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?

Thanks,
Liron

@lironbeni
Copy link
Author

Tried something like this:

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

    Arel::Nodes::SqlLiteral.new(joins(:member).select('members.first_name').first)

    # Arel::Nodes::SqlLiteral.new(
    #   Arel::SelectManager.join("genes.member_id = members.id",:members)
    # )
  end

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

Need help... :(

@lironbeni
Copy link
Author

This works fine when using simple search:

def self.member_member_name
    :member_first_name_or_member_last_name_cont
end

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
Copy link

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
end

@lironbeni
Copy link
Author

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}]
  end     

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

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
Copy link
Contributor

Any update on this? Also interested in this functionality.

@ernie
Copy link
Contributor

ernie commented Mar 12, 2014

+1

@radar
Copy link
Contributor

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
Copy link

graywh commented Mar 13, 2014

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

@lironbeni
Copy link
Author

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
Copy link
Contributor

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

@scarroll32
Copy link
Member

Closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants