user3278375
user3278375

Reputation: 63

Search Box model where

I have problem with model with use where.

I have in artists controller

  def search
@artists = Artist.where(params[:search])
end

and view artist

<%= form_tag(search_path) do %>
<%= search_field_tag(:search,"Search") %>
<%= submit_tag("Search") %>
<% end %>

In the partial view are all artists

And when I search something I have error.

SQLite3::SQLException: no such column: test1: SELECT "artists".* FROM "artists" WHERE (test1)

Upvotes: 1

Views: 50

Answers (2)

anyarms
anyarms

Reputation: 349

If you just want to search Artist.name, try changing your query to something like:

@artists = Artist.where("name = ?", params[:search])

That will produce a SQL query that looks like

SELECT "artists".* FROM "artists" WHERE ("name" = 'test1')

Instead of the one cited in your error message.

For more information on queries and how they work, you might want to look at: http://guides.rubyonrails.org/active_record_querying.html#conditions

Upvotes: 0

pangpang
pangpang

Reputation: 8821

Should be:

   @artists = Artist.where("name = ?", params[:search])

   @artists = Artist.where(name: params[:search]) # It is better

Upvotes: 1

Related Questions