Hello guys, I trying to create the conditions of my search, but I have some troubles. This is the method I''m trying to create in my model. def self.searchadv(title, place, category, date) !title.blank? ? conditions = [''title LIKE ?'', "%#{title}%"] : conditions = [] if conditions !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << [] else !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << [] end find(:all, :conditions => conditions)end I get this error wrong number of bind variables (4 for 1) in: title LIKE ? if I delete this: if conditions !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << []else !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << []end Everything works great, but I need this other options in order to create my search and I don''t undertand why the error is in the "LiKE" Does anyone could help me please? Thanks in advance! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/6upZgpqP5hMJ. For more options, visit https://groups.google.com/groups/opt_out.
On Dec 2, 6:26 pm, Jean <josor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello guys, > > I trying to create the conditions of my search, but I have some troubles. > > This is the method I''m trying to create in my model. > > def self.searchadv(title, place, category, date) > !title.blank? ? conditions = [''title LIKE ?'', "%#{title}%"] : conditions = []This (and the remainder) would probably be more readable if you used present? rather than !blank?> if conditions > !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << []I think you are confusing << with concat. This code appends a single item to conditions (that is a 0 or 2 item array) which isn''t what you intended. I''d probably write this as conditions += ['' and ...'', "%#{place}" if place.present? More readable (I think) would be fragments = [] bind_variables = [] if title.present? fragments << ''title LIKE ?'' bind_variables << title end If place.present? fragments << ''category like ?'' bind_variables << %#{place}%" ... end conditions = [fragments.join('' AND '')] + bind_variables Which removes some of the duplication you currently have. You might also want to look at something like squeel, which extends active record to make it easier to write this sort of query. Fred> !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << [] > else > !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << [] > end > find(:all, :conditions => conditions)end > > I get this error > > wrong number of bind variables (4 for 1) in: title LIKE ? > > if I delete this: > > if conditions > !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << []else > !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << []end > > Everything works great, but I need this other options in order to create my > search and I don''t undertand why the error is in the "LiKE" > > Does anyone could help me please? > > Thanks in advance!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Dec 2, 2012 at 4:26 PM, Jean <josorioe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def self.searchadv(title, place, category, date)Colour me doubtful, but since you''re passing in category and date, why are you using place in your category and date subclauses?> !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] > : conditions << [] > !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : > conditions << []I really like the way Fred puts it together by joining the fragments into the where clause. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Dec 3, 2012 at 6:26 AM, Jean <josorioe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello guys, > > I trying to create the conditions of my search, but I have some troubles. > > This is the method I''m trying to create in my model. > > def self.searchadv(title, place, category, date) > !title.blank? ? conditions = [''title LIKE ?'', "%#{title}%"] : conditions = [] > if conditions > !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << [] > else > !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << [] > end > find(:all, :conditions => conditions)end > > I get this error > > wrong number of bind variables (4 for 1) in: title LIKE ? > > if I delete this: > > if conditions > !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << []else > !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] > !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << []end > > Everything works great, but I need this other options in order to create > my search and I don''t undertand why the error is in the "LiKE" > > Does anyone could help me please? > > Thanks in advance! >you can also do it like this def self.searchadv(title, place, category, date) klass = scoped klass = klass.where(conditions for title here) if title.present? klass = klass.where(conditions for place here) if place.present? klass = klass.where(conditions for category here) if category.present? klass = klass.where(conditions for date here) if date.present? scopedend This is using rails 3. But you can still use this for rails 2.3 (i''m not sure what version they introduced this), you just have to change the where calls to scoped> -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/6upZgpqP5hMJ. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Dec 4, 2012 at 11:46 AM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Mon, Dec 3, 2012 at 6:26 AM, Jean <josorioe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hello guys, >> >> I trying to create the conditions of my search, but I have some troubles. >> >> This is the method I''m trying to create in my model. >> >> def self.searchadv(title, place, category, date) >> !title.blank? ? conditions = [''title LIKE ?'', "%#{title}%"] : conditions = [] >> if conditions >> !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << [] >> else >> !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << [] >> end >> find(:all, :conditions => conditions)end >> >> I get this error >> >> wrong number of bind variables (4 for 1) in: title LIKE ? >> >> if I delete this: >> >> if conditions >> !place.blank? ? conditions << ['' AND place LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' AND category LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' AND date LIKE ?'', "%#{place}%"] : conditions << []else >> !place.blank? ? conditions << ['' place LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' category LIKE ?'', "%#{place}%"] : conditions << [] >> !place.blank? ? conditions << ['' date LIKE ?'', "%#{place}%"] : conditions << []end >> >> Everything works great, but I need this other options in order to create >> my search and I don''t undertand why the error is in the "LiKE" >> >> Does anyone could help me please? >> >> Thanks in advance! >> > > you can also do it like this > > def self.searchadv(title, place, category, date) > > klass = scoped > > klass = klass.where(conditions for title here) if title.present? klass = klass.where(conditions for place here) if place.present? klass = klass.where(conditions for category here) if category.present? klass = klass.where(conditions for date here) if date.present? > > scopedend > > This is using rails 3. But you can still use this for rails 2.3 (i''m not > sure what version they introduced this), > you just have to change the where calls to scoped >the last line that calls scoped should be klass. my bad.> > > >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/6upZgpqP5hMJ. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.com >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.