Saturday 31 August 2013

Using a rails scope query in a controller

Using a rails scope query in a controller

I'm working on my first rails project, and I'm having trouble getting my
query to work correctly. My overall goal is to setup a collection of
scopes to create some long queries with multiple options. However, to
start with, I'm just trying to set up a single scope, to query a single
column from a single model. Here is my model:
class Drawing < ActiveRecord::Base
has_many :revisions
scope :by_description, lambda { |description| where(description:
description) unless description.nil? }
end
Here is the index action of my controller:
def index
@drawings = Drawing.by_description(params[:description]).all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @drawings }
end
end
And the view is the default index view for rails.
When I enter an input into the search field, for example "Case", the
submit button works and directs me to the index view, but all of the
"Drawings" are listed, instead of just the ones that have "Case" as their
description. Thanks so much for your help.

No comments:

Post a Comment