I am using Rails 2.3.5 and is wondering if it is possible to rewrite this query using conditions as a hash.
joined_deals = Deal.all :joins => :shops
:conditions => ["shops.name = ?", name]
to something like :conditions => {"shops.name" => name}. Is it possible in Rails 2?
Yes, it is possible in Rails 2.
For more information, refer here:
specifying-conditions-on-the-joined-tables
Related
I'm upgrading a Rails 3 code base to Rails 4 and have been updating to use the new scope style and querying interface.
I'm unsure how to switch over this scope that is using include as well as conditions in the scope's lambda.
scope :search_stuff, lambda { |search|
search_conditions = Array.new(4, "%#{search}%")
{
:include => {:sponsor => [], :routing_form_pi_users => [:user], :item => []},
:conditions => ["id like ? or title like ? or users.name like ? or sponsors.name like ?", *search_conditions]
}
}
When you compare the documentation about the query interface in different versions of the Rails Guide then you can see that the interface for eager loading of multiple associations didn't change that much.
The example in Rails 2.3.8 syntax:
Category.find 1, :include => {:posts => [{:comments => :guest}, :tags]}
The example for Rails 4.2 syntax:
Category.includes(articles: [{ comments: :guest }, :tags]).find(1)
Therefore it should be possible to just copy the old nested hash into the new syntax:
scope :search_stuff, lambda { |search|
includes(item: [], routing_form_pi_users: [:user], sponsor: []).
where('id like ? or title like ? or users.name like ? or sponsors.name like ?', *Array.new(4, "%#{search}%"))
}
I'm trying to combine an existing Rails query with a NOT query. I read that Rails 4 supports this well, but I'd like to know how to do it in Rails 3.
#next_question = Answer.where(:topic => subject, :result => session.question_pool, :user_id => current_user.id, 'practicesession_id != ?', params[:session_id]).first
I've tried the above and keep getting an error. The portion that relates to the NOT is the last bit of the query (practicesession_id). Any advice on how to do this?
In rails 3 this will work
#next_question = Answer.where(:topic => subject, :result => session.question_pool, :user_id => current_user.id)
.where(Answer.arel_table[:practicesession_id].not_eq(params[:session_id])).first
#next_question = Answer.where(topic: subject, result: session.question_pool, user_id: current_user)
.where('practicesession_id != ?', params[:session_id]).first
You can chain your wheres together, using AR syntax in some, and SQL strings in others.
I am trying to do a simple query with Rails 3 where clause.
I have the following:
Participant.find(:first, :conditions => ["participants.role = ? AND participants.board_id = ?", "Manager", board.id])
Which works very well. I am trying to rewrite it the Rails 3 way as follows:
Participant.where(:board => board, :role => "Manager")
However this does not work. Is there a way to stipulate first with the where to get the same return as above?
Participant.where(:board_id => board.id, :role => "Manager").first
I am using Ruby on Rails 3 and I would like to limit a search in a where method from ActiveRecord::QueryMethods using something as the following
Users.where(:name => "Test_name", :limit => 10)
Users.where(:name => "Test_name").limit(10)
That is, I would like to query only 10 records. How I can do that?
In the RoR source code there is:
def where(opts, *rest)
relation = clone
relation.where_values += build_where(opts, rest) unless opts.blank?
relation
end
Your second example works:
Users.where(:name => "Test_name").limit(10)
There is a nice list and explanation of all the query methods in this Rails guide: http://guides.rubyonrails.org/active_record_querying.html
I don't think you can do that in where. You can only do
Users.where(:name => "Test_name").limit(10)
or if you insist to use :limit in other selector method:
Users.find(:all, :conditions => ["name = 'Test_name'"], :limit => 10)
Imagine I have wiki Articles, with many Revisions. I'd like to do a query with ActiveRecord through the database, which only returns those Articles which have Revisions which are updated in the past 24 hours. Is such a thing possible?
I'd imagine it'd be something like:
Articles.find_all(:include => :revisions,
:conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc})
If this isn't possible, is there some type of raw SQL I could pass to find_by_SQL that'd do the trick?
If you only need the article and don't need eager loading of all the revisions, you could use :joins instead. It uses less memory because it's not preloading all the revisions for each article. Use :include though if you will be doing something like article.revisions.
Article.find(:all,
:joins => :revisions,
:conditions => ["revisions.updated_at > ?", 24.hours.ago]
)
Depending on how exactly you have your models named, it's going to be something like this.
Article.find(:all, :include => :revisions, :conditions => ["revisions.updated_at > ?", 1.week.ago])