Find with condition in Rails
You can use .where.not
:
<% Event.where.not(id: @seats.map(&:event_id)).each do |p| %>
<...>
<% end %>
Rails: Find all with conditions
Try this.
@fromcanada = User.find(:all, :conditions => { :country => 'canada' })
edit:
As jason328 pointed out, the above answer is deprecated in 3.2, and an updated answer would be
@fromcanada = User.where(:country => 'canada')
.find or where condition in active record querying
These are all equivalent. What you're seeing here is the evolution of the ActiveRecord query syntax from before AREL was incorporated. The older style dynamic finders are still valid, though.
This syntax is from ROR 2.x and earlier using dynamic finders:
FileOrFolder.find_by_fullpath(completePath, :select=>"id")
Whereas these are more in the ROR 3.x style:
FileOrFolder.where(fullpath: completePath).select(:id).first
Component.where(cluster_id: cluster_id, name: key).first
And your last example using find
is valid in either context.
Component.find(:first, :conditions=>["cluster_id = ? AND name = ?", cluster_id, key])
When in doubt, consult the ROR query guide.
I personally find the where
styles are very useful when you're building up a query over several lines of code and not all at once. Since they defer execution until the latest moment, they let you build the query piecemeal.
Rails 4 - How to get specific records based on where conditions
First of all you can't pass the Date.today
as a string to the query, it will be passed to the database and it won't understand it.
The query should be something like this
@sale = Sale.where('offer_end > ?', Date.today)
The Date.today
will be evaluated then passed as a value to the query.
You could replace the Date.today
with any date object or date string, which in your case seems to be in theparams[:offer_end]
@sale = Sale.where('offer_end > ?', params[:offer_end])
rails 4 how to use where and where in condition simultaneously
model_ids = model.split(",").map(&:to_i)
@posts = Post.where(category_id: id, product_model_id: model_ids)
or
model_ids = model.split(",").map(&:to_i)
@posts = Post.where("category_id = ? AND product_model_id IN (?)", id, model_ids)
Rails Search with multiple Conditions
You could just create an initial query and chain on it while you go, from what I can see the order
part is common and always present, so use it as the base query
@event_results = Event.order(event_date: :asc)
@event_results = @event_results.where('event_results.name LIKE ?', "%#{params[:query]}%") if params[:query]
@event_results = @event_results.joins(:host).where(host_type: params[:host_type]) if proper_condition
@event_results = @event_results.more_methods if more_conditions
#etc
@event_results #the return
By default ruby will return the last statement, so you don't need to use return
, unless the last statement returns something like true/false
then you should just add a new line with the variable @event_results
NOTE: never use input directly from params into your query, you're allowing people to inject sql to your code, use the substitution
method so thatActiveRecord
would sanitize the input for you.Bad:
where("name LIKE '%#{params[:query]}%'")
Good:
where('name LIKE ?', "%#{params[:query]}%")
Related Topics
Why Is My Left Join Not Returning Nulls
Sql: How to Order Null and Empty Entries to The Front in an Orderby
Mybatis Rowbounds Doesn't Limit Query Results
Maximum Length of an SQL Query
Order by Month and Year in SQL with Sum
Sql Server Store Multiple Values in SQL Variable
Oracle - Clone Table - Structure, Data Constraints and All
Best Practices for Multithreaded Processing of Database Records
How to Force MySQL to Perform Subquery First
How to Convert Integer to Decimal in SQL Server Query
Counter_Cache Has_Many_Through SQL Optimisation, Reduce Number of SQL Queries
Pure-SQL Technique for Auto-Numbering Rows in Result Set
Copy Data from One Existing Row to Another Existing Row in SQL
Sql Query to Find Last Day of Current Month
Calling Shell Script from Pl/Sql, But Shell Gets Executed as Grid User, Not Oracle