Is it possible to us an if statement in a find condition statement
Controller:
def index
@companies = Company.search(params[:province])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @companies }
end
end
Model:
def self.search(search)
if search
find(:all, :conditions => [''province LIKE ?'',
"%#{search}%"])
else
find(:all)
end
end
I want to be able to check the URL to see if there are any other
parameters being passed and if so use them in the condition statemetn
find(:all, :conditions => [''province LIKE ? and wood_based_panels
1'', "%#{search}%"])
I would like to check to see if the wood_based_panels variable was
passed in the URL if so I would like to add it to the statement if not
I would like to remove it and only have the following.
find(:all, :conditions => [''province LIKE ?,
"%#{search}%"])
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Shawn B wrote:> Is it possible to us an if statement in a find condition statement > > Controller: > def index > @companies = Company.search(params[:province]) > respond_to do |format| > format.html # index.html.erb > format.xml { render :xml => @companies } > end > end > > Model: > def self.search(search) > if search > find(:all, :conditions => [''province LIKE ?'', "%#{search}%"]) > else > find(:all) > end > endFirstly, always remember these are statements with variables. You can pull the above apart like this: def self.search(search) condition = [''province LIKE ?'', "%#{search}%"] if search find(:all, :conditions => condition) end> I want to be able to check the URL to see if there are any other > parameters being passed and if so use them in the condition statemetnThen don''t pass in params[:province]. Pass in params, and use it directly in a named argument replacement thing, like this (someone check my math!): condition = ''1 = 1'' params.each do |k,v| condition << " AND #{k} LIKE ''%:#{k}%''" end return find(:all, :conditions => [condition, params]) That way, whatever new params you think of, the method will automatically accept. (This logic assumes LIKE ''%%'' matches anything. Write unit tests to check that. Otherwise, put unless v.blank? on the end of the line with the <<. -- Phlip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the quick response. I am very new to Ruby and wondering if
you can help me a little bit more on this topic.
I tried your suggestions as follows
New Controller:
def index
@companies = Company.search(params[])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @companies }
end
end
New Method:
def self.search(search)
condition = ''1 = 1''
params.each do |k,v|
condition << " AND #{k} = '':#{k}''"
end
return find(:all, :conditions => [condition, params])
condition = [''province LIKE ?'', "%#{search}%"]
if search
find(:all, :conditions => condition)
end
I am getting an error now as follows:
wrong number of arguments(0 for 1)
On Mar 25, 9:15 am, Phlip
<phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Shawn B wrote:
> > Is it possible to us an if statement in a find condition statement
>
> > Controller:
> > def index
> > @companies = Company.search(params[:province])
> > respond_to do |format|
> > format.html # index.html.erb
> > format.xml { render :xml => @companies }
> > end
> > end
>
> > Model:
> > def self.search(search)
> > if search
> > find(:all, :conditions => [''province LIKE
?'', "%#{search}%"])
> > else
> > find(:all)
> > end
> > end
>
> Firstly, always remember these are statements with variables. You can pull
the
> above apart like this:
>
> def self.search(search)
> condition = [''province LIKE ?'',
"%#{search}%"] if search
> find(:all, :conditions => condition)
> end
>
> > I want to be able to check the URL to see if there are any other
> > parameters being passed and if so use them in the condition statemetn
>
> Then don''t pass in params[:province]. Pass in params, and use it
directly in a
> named argument replacement thing, like this (someone check my math!):
>
> condition = ''1 = 1''
> params.each do |k,v|
> condition << " AND #{k} LIKE
''%:#{k}%''"
> end
> return find(:all, :conditions => [condition, params])
>
> That way, whatever new params you think of, the method will automatically
accept.
>
> (This logic assumes LIKE ''%%'' matches anything. Write
unit tests to check that.
> Otherwise, put unless v.blank? on the end of the line with the <<.
>
> --
> Phlip
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---