Rails - Find with Condition in Rails 4

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 that ActiveRecord would sanitize the input for you.

Bad:

where("name LIKE '%#{params[:query]}%'")

Good:

where('name LIKE ?', "%#{params[:query]}%")


Related Topics



Leave a reply



Submit