Question

Looking to limit a find based on conditions in a Rails 2.0.2.

Find statement:

@employees = Employee.find_by_contents(params[:keywords].to_s, :include => [ :categories, :revisions, :approvals, :archives, :related_documents ])

Need to add a condition to limit find

:conditions=>["archived = '0'"]

Though this doesn't work

@employees = Employee.find_by_contents(params[:keywords].to_s, :include => [ :categories, :revisions, :approvals, :archives, :related_documents ], :conditions=>["archived = '0'"])

Anyone know what the syntax should be?

Was it helpful?

Solution 2

Was having quite a time with adding a condition to ferret find_by, so I added a post command to filter if it's archived. If you think of a better way still, let me know. Thanks.

@employees = Employee.find_by_contents(params[:keywords].to_s, 
              :include => [ :categories, :revisions, :approvals, 
                            :archives, :related_documents ])
@employees = @employees.find_all {|p| !p.archived}

OTHER TIPS

If you pass conditions an array, I think it needs to be for a parameterized condition. Try either/both of the following:

    @employees = Employee.find_by_contents(params[:keywords].to_s, 
                  :include => [ :categories, :revisions, :approvals, 
                                :archives, :related_documents ], 
                  :conditions=> "archived = '0'")

or

@employees = Employee.find_by_contents(params[:keywords].to_s,                           
                  :include => [ :categories, :revisions, :approvals, 
                                :archives, :related_documents ],
                  :conditions=>["archived = ?",'0'])

Firstly, what error do you have?

And try this

@employees = Employee.find_by_contents(params[:keywords].to_s, :include => [ :categories, :revisions, :approvals, :archives, :related_documents ], :conditions=>["employees.archived = ?", false])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top