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%
Rails .where any field contains specific text
I do not know of a framework-way to do so. You could code something using
my_attributes = YourModel.attributes
# delete attributes you do not need, like `id` etc.
# or just create an array with your desired attributes,
# whichever way is faster
queries = my_attributes.map { |attr| "#{attr} LIKE %insert_your_text_here%" }
# Do not use this if the text your looking for is provided by user input.
built_query = queries.join(" OR ")
YourModel.where(built_query)
This could bring you closer to your goal. Let me know if this makes sense to you.
edit: The answer https://stackoverflow.com/a/49458059/299781 mentions Ransack. That's a nice gem and takes the load off of you. Makes it easier, nicer and performs better :D
Glad you like this, but pay attention that you make your app open for sql injection, if you take user-input as the text you are looking for. (with this solution) Ransack would alleviate that.
rails find records containing specific word
You can try this
search_term = "string"
tables = Table.where("tables.content LIKE ?", "%#{search_term}%")
or, if you want to ignore case sensitiveness, then do this
search_term = "string"
tables = Table.where("lower(content) LIKE lower(?)", "%#{search_term}%")
Hope this helps!
Rails find by part of string
You can do it using LIKE
:
User.where('name LIKE ?', "%#{@user.name}%")
The syntax for a LIKE
sql statement to find a field for a given string fragment is to surround your string fragment with %
characters, so the resulting SQL will look something like this
"SELECT \"users\".* FROM \"users\" WHERE (name LIKE '%Rob Phillips%')"
can you use activerecord to find substring of a field? (quick & dirty keyword finder)
Yeah, just use a LIKE statement in MySQL.
In Rails 2.x:
Table.find(:all, :conditions => ['keywords LIKE ?', '%crescent%'])
In Rails 3.x:
Table.where('keywords LIKE ?', '%crescent%').all
How to use find_by option in rails
You'd use this to find a record that matches just the city_name
field:
City.find_by_city_name(city_name)
You'd use this to find a record that matches the city_name
& country_name
fields:
City.find_by_city_name_and_country_name(city_name, country_name)
You probably wouldn't search for a record that matches all fields, because if you have the ID, then you can just get it directly with that:
City.find(id)
Search this page for "find_by" for more details.
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.
Related Topics
How to Write This SQL Query in Mongodb Syntax
Pass Multiple Sets or Arrays of Values to a Function
Randomly Select a Row with SQL in Access
SQL Error: "Name Already Used by an Existing Constraint"
How to Copy Data from One Table to Another in Postgres Using Copy Command
Full-Text Search SQL Server 2005
Oledb Case When in Select Query
General Rules for Simplifying SQL Statements
Finding the Data Types of a SQL Temporary Table
How to Get Rid of #Temp Tables from the Query
What Is the Optimal Way to Compare Dates in Microsoft SQL Server
Should the Data Access Layer Contain Business Logic
SQL Server Return the Value of Identity Column After Insert Statement
Access SQL Query: Find the Most Recent Date Entry for Each Employee for Each Training Course
Is Golang's SQL Package Incapable of Ad Hoc/Exploratory Queries
Trying to Flatten Rows into Columns