I'm using Ransack with Rails 3.
My views:
<%= search_form_for @search do |f| %>
<%= f.text_field :first_name_or_last_name_or_company_cont, :style => 'width:150px;padding-top:6px;' %><p class="button">
<%= f.submit "Search by name or company" %></p>
<% end %>
My schema:
create_table "users", :force => true do |t|
t.string "first_name",
t.string "last_name"
on the User model I have:
def full_name
"#{self.first_name} #{self.last_name}"
end
Working on the console.
If I have user with first_name = "Foo"
, last_name = "Bar"
, when I search with either one, I get the results as expected.
When I search with full_name = "Foo Bar"
, I get no results.
I tried changing my text_field for Ransack to:
<%= f.text_field :first_name_or_last_name_or_company_or_full_name_cont
I get first_name_or_last_name_or_company_or_full_name_cont is undefined
is there a quick way to solve this without adding another column for full_name on my users table?
also :
7: def index
8: search_params = params[:q]
=> 9: binding.pry
[1] pry(#<UsersController>)> search_params
=> {"first_name_or_last_name_or_company_cont"=>"Foo Bar"}
I guess I can split key value here to search the first name only.
You have to use a
Ransacker
. Add this in yourUser
model:You can check the Ransack GitHub wiki to more examples.