1

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?

Jeffrey
  • 4,098
  • 11
  • 42
  • 66

3 Answers3

0

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'])
Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40
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])
fl00r
  • 82,987
  • 33
  • 217
  • 237
0

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}
Jeffrey
  • 4,098
  • 11
  • 42
  • 66