开发者

Rails: find_by_contents with conditions; syntax help please

开发者 https://www.devze.com 2023-02-08 10:07 出处:网络
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, :ar

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_docum开发者_运维问答ents ], :conditions=>["archived = '0'"])

Anyone know what the syntax should be?


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'])


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}


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])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号