search not working right

32 views
Skip to first unread message

fugee ohu

unread,
Dec 7, 2015, 8:55:15 AM12/7/15
to Ruby on Rails: Talk
when the search gets submitted it only returns results for records that had the searched value in publicists

from my model:
 def self.search(search)
  where("headline LIKE ?", "%#{search}%")
  where("storyline LIKE ?", "%#{search}%")
  where("publicist LIKE ?", "%#{search}%")
 end

from my  controller:
def index
  if params[:search]
    @press_releases = PressRelease.search(params[:search]).order("dateline DESC")
  else
    @press_releases = PressRelease.order("dateline DESC")
  end
end

Colin Law

unread,
Dec 7, 2015, 9:17:13 AM12/7/15
to Ruby on Rails: Talk
On 7 December 2015 at 13:55, fugee ohu <fuge...@gmail.com> wrote:
> when the search gets submitted it only returns results for records that had
> the searched value in publicists
>
> from my model:
> def self.search(search)
> where("headline LIKE ?", "%#{search}%")
> where("storyline LIKE ?", "%#{search}%")
> where("publicist LIKE ?", "%#{search}%")
> end

The where method returns a set of matching records, your method runs
the first where() and throws the result away, then it does the same
with the second and throws the result away, then it runs the third and
returns that. You presumably wish to find records where any of three
are true so you need to use something like
where(("headline LIKE ? OR storyline LIKE ? OR publicist LIKE ?",
"%#{search}%", "%#{search}%", "%#{search}%")

Colin

fugee ohu

unread,
Dec 7, 2015, 11:15:18 AM12/7/15
to Ruby on Rails: Talk
Thanks Colin, the person who posted that code sure had me fooled, i could have done better myself without adding code to the model It looked like an advanced technique, who would have guessed it was so wrong

Colin Law

unread,
Dec 7, 2015, 11:27:14 AM12/7/15
to Ruby on Rails: Talk
On 7 December 2015 at 16:15, fugee ohu <fuge...@gmail.com> wrote:
> Thanks Colin, the person who posted that code sure had me fooled, i could
> have done better myself without adding code to the model It looked like an
> advanced technique, who would have guessed it was so wrong

Where did you see it posted? Have you still got the link?

Colin

>
> On Monday, December 7, 2015 at 9:17:13 AM UTC-5, Colin Law wrote:
>>
>> On 7 December 2015 at 13:55, fugee ohu <fuge...@gmail.com> wrote:
>> > when the search gets submitted it only returns results for records that
>> > had
>> > the searched value in publicists
>> >
>> > from my model:
>> > def self.search(search)
>> > where("headline LIKE ?", "%#{search}%")
>> > where("storyline LIKE ?", "%#{search}%")
>> > where("publicist LIKE ?", "%#{search}%")
>> > end
>>
>> The where method returns a set of matching records, your method runs
>> the first where() and throws the result away, then it does the same
>> with the second and throws the result away, then it runs the third and
>> returns that. You presumably wish to find records where any of three
>> are true so you need to use something like
>> where(("headline LIKE ? OR storyline LIKE ? OR publicist LIKE ?",
>> "%#{search}%", "%#{search}%", "%#{search}%")
>>
>> Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/cf20186b-6a84-4ecb-b0b2-f44603424b16%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

James Jelinek (shakycode)

unread,
Dec 7, 2015, 11:34:15 AM12/7/15
to rubyonra...@googlegroups.com
Also in the previous code, you can do ILIKE instead of like to get rid case sensitivity.  Just a FYI :)

fugee ohu

unread,
Dec 7, 2015, 12:34:38 PM12/7/15
to Ruby on Rails: Talk
>> Where did you see it posted?  Have you still got the link?
>> Colin


Colin Law

unread,
Dec 7, 2015, 12:52:31 PM12/7/15
to Ruby on Rails: Talk
If you read the comments you will see that others have pointed out the
error, though why it has not been corrected is beyond me.

Cheers

Colin
Reply all
Reply to author
Forward
0 new messages