I am currently writing a search method for my rails applications and at
the moment it works fine. I have the following in my game.rb:

def self.search(search)
if search
find(:all, :conditions => ['game_name LIKE ? OR genre LIKE ? OR
console LIKE ?', "%#{search}%", "#{search}", "#{search}"])
else
find(:all)
end
end

No that searches fine but my problem is that if there is a record in
game_name that has the words playstation in it will finish the search
there and only return that record rather than that as well as all games
that have playstation stored in console. Now I understand that is
because I have OR in my conditions but I don't know an alternative. AND
simply requires all the conditions to match or no return at all. What is
an alternative I can use to AND and OR. Help would be much appreciated.

If there is a solution that has seperate search boxes and entries then
that would be fine, I don't necessarily require the search to find it
all based on one search form.

--
Posted via http://www.ruby-forum.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@googlegroups.com.
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.

Search Discussions

  • Roger Patrick at Feb 21, 2012 at 3:44 pm
    I am currently writing a search method for my rails applications and at
    the moment it works fine. I have the following in my game.rb:

    def self.search(search)
    if search
    find(:all, :conditions => ['name LIKE ? OR genre LIKE ? OR
    console LIKE ?', "%#{search}%", "#{search}", "#{search}"])
    else
    find(:all)
    end
    end

    Now that searches fine but my problem is that if there is a record in
    game_name that has the words playstation in it will finish the search
    there and only return that record rather than that as well as all games
    that have playstation stored in console. Now I understand that is
    because I have OR in my conditions but I don't know an alternative. AND
    simply requires all the conditions to match or no return at all. What is
    an alternative I can use to AND and OR. Help would be much appreciated.

    If there is a solution that has seperate search boxes and entries then
    that would be fine, I don't necessarily require the search to find it
    all based on one search form.

    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
  • Tom Meinlschmidt at Feb 21, 2012 at 3:58 pm
    IF you really need to search over several columns, use concat() for db columns... like

    scope :search, labda{|search| where("concat(name, genre, console) like ?", "%#{search}%").presence || all }

    and then use

    Model.search('value')

    tom
    On Feb 21, 2012, at 16:44 , Roger Patrick wrote:

    I am currently writing a search method for my rails applications and at
    the moment it works fine. I have the following in my game.rb:

    def self.search(search)
    if search
    find(:all, :conditions => ['name LIKE ? OR genre LIKE ? OR
    console LIKE ?', "%#{search}%", "#{search}", "#{search}"])
    else
    find(:all)
    end
    end

    Now that searches fine but my problem is that if there is a record in
    game_name that has the words playstation in it will finish the search
    there and only return that record rather than that as well as all games
    that have playstation stored in console. Now I understand that is
    because I have OR in my conditions but I don't know an alternative. AND
    simply requires all the conditions to match or no return at all. What is
    an alternative I can use to AND and OR. Help would be much appreciated.

    If there is a solution that has seperate search boxes and entries then
    that would be fine, I don't necessarily require the search to find it
    all based on one search form.

    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
    --
    ===============================================================================
    Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

    www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
    ===============================================================================

    --
    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@googlegroups.com.
    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.
  • Roger Patrick at Feb 21, 2012 at 6:34 pm
    Hey I found out my problem, it was because I did not have the % symbol
    in the last two search calls, all works fine now.

    Do you know how I would be able to make it as three separate search
    boxes and buttons instead of all going in to one?

    All I know is how to search every column under one form but would like
    to do it as three separate forms. Not like in the advance search by
    railscast but three text fields with three submit buttons.

    Thanks
    R. Patrick

    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
  • Walter Lee Davis at Feb 21, 2012 at 6:48 pm

    On Feb 21, 2012, at 1:34 PM, Roger Patrick wrote:

    Hey I found out my problem, it was because I did not have the % symbol
    in the last two search calls, all works fine now.

    Do you know how I would be able to make it as three separate search
    boxes and buttons instead of all going in to one?

    All I know is how to search every column under one form but would like
    to do it as three separate forms. Not like in the advance search by
    railscast but three text fields with three submit buttons.

    Thanks
    R. Patrick
    Take a serious look at ransack. It's a very neat search gem, and it allows you to create specialized search fields just by naming those fields according to a dsl. So your controller would look like this:

    @q = ModelName.search(params[:q])
    @model_names = @q.result(:distinct => true)

    No changes needed no matter what sort of hijinks you get up to in your view:

    <%= search_form_for @q do |f| %>
    <%= f.search_field :foo_cont, :class => 'search' %> (foo contains the search parameter)
    <%= f.search_field :bar_start, :class => 'search' %> (bar starts with the search parameter)
    <%= f.submit :name => nil, :value=> 'Search' %>
    <% end %>

    It contains the following predicates, which you just tail on to the name of the field you wish to search:

    eq
    not_eq
    matches
    does_not_match
    lt
    lteq
    gt
    gteq
    in
    not_in
    cont
    not_cont
    start
    not_start
    end
    not_end
    true
    false
    present
    blank
    null
    not_null

    And if that's not enough, you can create your own very directly.

    Walter
    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
    --
    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@googlegroups.com.
    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.
  • Roger Patrick at Feb 21, 2012 at 7:58 pm

    Take a serious look at ransack. It's a very neat search gem, and it
    allows you to create specialized search fields just by naming those
    It sounds really good Walter, do you have any links for Ransack, I just
    googles ransack - rails but it hasn't provided me with a documentation
    link.

    Thanks
    Patrick

    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
  • Peter De Berdt at Feb 21, 2012 at 8:09 pm

    On 21 Feb 2012, at 20:58, Roger Patrick wrote:

    Take a serious look at ransack. It's a very neat search gem, and it
    allows you to create specialized search fields just by naming those
    It sounds really good Walter, do you have any links for Ransack, I
    just
    googles ransack - rails but it hasn't provided me with a documentation
    link.
    https://github.com/ernie/ransack

    Best regards

    Peter De Berdt

    --
    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@googlegroups.com.
    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.
  • Roger Patrick at Feb 22, 2012 at 3:43 pm
    Hey I installed the ransack gem and have added some basic code to my
    code but have received an error now on my page which is the followin

    undefined method `result' for #<Array:0x44cd540>

    I have the following code in my gamescontroller.rb

    def index
    @q = Game.search(params[:q])
    @game = @q.result(:distinct => true)
    end


    and the following code in my games/index.html.erb

    <%= search_form_for @q do |f| %>

    <%= f.label :game_name_start %>
    <%= f.text_field :game_name_start %>
    <%= f.submit %>
    <% end %>

    any ideas of what the problem might be?

    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
  • Walter Lee Davis at Feb 22, 2012 at 3:49 pm

    On Feb 22, 2012, at 10:43 AM, Roger Patrick wrote:

    Hey I installed the ransack gem and have added some basic code to my
    code but have received an error now on my page which is the followin

    undefined method `result' for #<Array:0x44cd540>

    I have the following code in my gamescontroller.rb

    def index
    @q = Game.search(params[:q])
    @game = @q.result(:distinct => true)
    end


    and the following code in my games/index.html.erb

    <%= search_form_for @q do |f| %>

    <%= f.label :game_name_start %>
    <%= f.text_field :game_name_start %>
    <%= f.submit %>
    <% end %>

    any ideas of what the problem might be?
    Didn't you have a search method that you wrote earlier? That may be overriding the one in the Ransack gem. Ransack returns an Arel collection, not an array.

    Walter
    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
    --
    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@googlegroups.com.
    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.
  • Roger Patrick at Feb 22, 2012 at 4:20 pm

    Walter Davis wrote in post #1048250:
    On Feb 22, 2012, at 10:43 AM, Roger Patrick wrote:

    end

    any ideas of what the problem might be?
    Didn't you have a search method that you wrote earlier? That may be
    overriding the one in the Ransack gem. Ransack returns an Arel
    collection, not an array.

    Walter
    I still have a method in the model but I have removed the old search
    from the index.html.erb. So I can't see that being the problem.

    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
  • Walter Lee Davis at Feb 22, 2012 at 4:45 pm

    On Feb 22, 2012, at 11:19 AM, Roger Patrick wrote:

    Walter Davis wrote in post #1048250:
    On Feb 22, 2012, at 10:43 AM, Roger Patrick wrote:

    end

    any ideas of what the problem might be?
    Didn't you have a search method that you wrote earlier? That may be
    overriding the one in the Ransack gem. Ransack returns an Arel
    collection, not an array.

    Walter
    I still have a method in the model but I have removed the old search
    from the index.html.erb. So I can't see that being the problem.
    Try removing or renaming the search method in the model. I am guessing that that is clobbering the search method that Ransack is injecting into your model by default.

    Walter
    --
    Posted via http://www.ruby-forum.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@googlegroups.com.
    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.
    --
    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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 21, '12 at 3:42p
activeFeb 22, '12 at 4:45p
posts11
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase