ActiveRecord find starts with
I would highly recommend the Searchlogic plugin.
Then it's as easy as:
@search = Model.new_search(params[:search])
@search.condition.field_starts_with = "prefix"
@models = @search.all
Searchlogic is smart enough, like ActiveRecord, to pick up on the field name in the starts_with
condition. It will also handle all pagination.
This method will help prevent SQL injection and also will be database agnostic. Searchlogic ends up handling the search differently depending on the database adapter you're using. You also don't have to write any SQL!
Searchlogic has great documentation and is easy to use (I'm new to Ruby and Rails myself). When I got stuck, the author of the plugin even answered a direct email within a few hours helping me fix my problem. I can't recommend Searchlogic enough...as you can tell.
ActiveRecord find records that start with one of the words in an array
This should do it:
prefixes = ['mega', 'hyper', 'super']
sql_conditions = prefixes.map{ |prefix| "field ILIKE #{sanitize("#{prefix}%")}" }.join(' OR ')
MyModel.where(sql_conditions)
In my IRB console:
prefixes = ['mega', 'hyper', 'super']
sql_conditions = prefixes.map{ |prefix| "field ILIKE #{ActiveRecord::Base.sanitize("#{prefix}%")}" }.join(' OR ')
# => "field ILIKE 'mega%' OR field ILIKE 'hyper%' OR field ILIKE 'super%'"
An alternative, for MySQL only:
prefixes = ['^mega', '^hyper', '^super'] # the ^ wildcard represents the start of line
sql_conditions = "field RLIKE '#{prefixes.join('|')}'" # RLIKE is also known as REGEXP
MyModel.where(sql_conditions)
Rails ActiveRecord - find by substring of another string
Yes, this is possible in Ruby on Rails and SQL. Depends a bit on the database you use, but something like this should work:
Modelname.where("? LIKE CONCAT('%', name, '%')", 'Ttomas')
Rails Active Record starts with
Following this link: http://programming-tut.blogspot.ca/2009/10/ruby-on-rails-loop-alphabet-to-z.html
You could do that:
range = [ 'A', 'E' ]
conditions = (range.first..range.last).to_a.map{ |letter| " name ILIKE '#{letter}%' " }.join('OR')
Item.where(conditions)
Or like Vimsha pointed out:
letters = ('A'..'E').to_a
Item.where("substr(name, 1, 1) IN (?)", letters)
Rails: How to find_by a field containing a certain string
I think something like this should work:
Topic.where("name like ?", "%apple%")
To accomodate for your edit:
Topic.where("name like ?", "%#{@search}%")
Basic string interpolation, you're using the value of @search
inside the string %%
, so you @search = "apple"
then you end up with %apple%
How to perform an active record search based on a regular expression
.find(_by)
only returns the first match. Since you want to find all users with a Gmail address you need to use .where
.
You can easily combine the query with some regex:
With Mysql, use REGEXP
User.where("email REGEXP ?", '.*@gmail.com')
With Postgres, use regexp_matches
:
User.where("regexp_matches(email, ?)", '.*@gmail.com')
Related Topics
Ruby Webrick Http Authentication
Sending Form Data to Remote Rails Application Using Httparty
Regex to Extract Boundary and Content Type Out of Mail Headers
Use Some Middleware Only for Specific Rack Website
Rails Cors Issue with Ajax API Endpoint Request
Rails Console Is Adding Nil Instead of Values
Bug in Implemented Tagging System
Activerecord::Recordnotfound in Articlescontroller#Show Couldn't Find Article Without an Id
Ruby - Permutation Between Elements of an Array
Rails Contact Form Not Working
How to Access Ruby Method Inside Module and Inside a Class from Another .Rb File
How to Set Text Area Properties Inside Controller Page Using Ror
What Is a Very Simple Authentication Scheme for Sinatra/Rack
Jruby on Rails VS. Ruby on Rails, What's Difference
Rails: Logging the Entire Stack Trace of an Exception