I have the following sql query and I want to map it to a rails count query:
SELECT count(*), DATE(CONVERT_TZ(created_at, '+00:00', '-05:00')) as converted_date
FROM video_logs
where user_id = 19 and question_id = 96 and dashboard = 'player_question'
GROUP BY converted_date;
How do I do this?
In rails 3:
VideoLog.select("count(*), DATE(CONVERT_TZ(created_at, '+00:00', '-05:00')) as converted_date").\
where(:user_id => 19, :question_id => 96, :dashboard => 'player_question').\
group('converted_date')
In rails 2:
VideoLog.all(:select => "count(*), DATE(CONVERT_TZ(created_at, '+00:00', '-05:00')) as converted_date",
:conditions => {:user_id => 19, :question_id => 96, :dashboard => 'player_question'},
:group => 'converted_date')
Related
In model:
def self.get_by_slug(slug)
self.where("slug = ?", slug)
end
In controller:
#route: match '/category/:slug', :to => 'category#index', :as => "category_jobs"
#category = Category.get_by_slug(params[:slug])
#jobs = Job.where("category_id = ? AND expires_at > ?", #category.id, DateTime.now)
.paginate(:page => params[:page], :per_page => Jobeet::Application::MAX_JOBS_ON_CATEGORY_PAGE)
.order("expires_at desc")
When I trying get category.id in controller I am getting error:
undefined method `id' for #
Could somebody give me any advice?
If you expect a single record you should do:
#category = Category.get_by_slug(params[:slug]).first
because .where(something) doesn't return a single record.
Hey I'm trying to figure something out..
I want to get all Carts that have a cart_stage.stage equal to '35' (this is a separate table and a cart has many stages)
the cart_stage table is a bit like
id ----- cart_id ----- stage
1 ------- 123 ---------- 20
2 ------- 123 ---------- 35
3 ------- 102 ---------- 35
I am trying this at the moment:
# Cart model
has_one :top_stage, :foreign_key => 'cart_id', :class_name => "CartStage", :order => 'stage'
# Cart controller
#carts = Cart.find :all, :order => 'created_at DESC', :include => :top_stage, :conditions => ["top_stage.stage = ?", 35]
This gives me :
SQLite3::SQLException: no such column: top_stage.stage: SELECT DISTINCT "carts".id FROM "carts" WHERE (top_stage.stage = 35) ORDER BY created_at DESC LIMIT 40 OFFSET 0
Hope it all makes sense and any help would be greatly appreciated,
Alex
That should probably be:
#carts = Cart.find(:all, :order => 'carts.created_at DESC', :include => :top_stage, :conditions => { "cart_stages.stage" => 35 })
Remember that you use the name of the table in the conditions, not the name of the association, however you do use the name of the association in the include option.
Whenever possible, you should probably use the hash method for expressing conditions to keep your declarations simple. It's when you need complicated OR or > type logic that the array-style proves necessary.
A better way of expressing it for Rails 3 is:
#carts = Cart.order('carts.created_at DESC').include(:top_stage).where('top_stages.stage' => 35)
It's not clear why your cart_stages table isn't being added with a JOIN.
Anywhere that you include literal SQL, you need to use the real table and column names, not the Rails association names. So you need to have: :conditions => ["cart_stage.stage = ?", 35] instead of top_stage
I managed to work it out my self actually.. What I wanted was -
#carts = Cart.find :all, :order => 'created_at DESC', :joins => :top_stage, :conditions => { :cart_stages => { :stage => 35 } }
Thanks for all the help guys! :)
The following query returns the collection of AR objects that I want to update:
Variant.all(:joins => { :candy_product => :candy }, :conditions => "candies.name = 'Skittles'")
I'm trying to do something like the following:
Variant.update_all(:price => 5, :joins => { :candy_product => :candy }, :conditions => "candies.name = 'Skittles'")
This should only update the price for the variants returned from original query. Is this possible with AR or will I have to write the SQL? This is a pretty large collection, so anything that iterates is out.
Using Rails 2.3.4.
As #François Beausoleil pointed correctly we should use scoped
Variant.scoped(:joins => { :candy_product => :candy }, :conditions => "candies.name = 'Skittles'").update_all(:price => 5)
on my site, I'm trying to display the hiking trails with the latest posted pictures (Scroll to the "Picture section: http://www.trailheadfinder.com/trail_search/latest_trails). However, the current query I use, is showing the trails in order of the "first" picture posted. So when a new picture is added at a later date, the trail does not show at the top. I have a trail table and a trailpicture table that are linked. Here is the current query I use:
#trails_pictures = Trailpicture.find(:all,
:limit => 20,
:include => [:trail],
:select => 'trailpictures.trail_id, trails.name, trails.short_description, trails.city, trails.state, trails.country',
:group => 'trailpictures.trail_id',
:conditions => ["trailpictures.parent_id is NULL"],
:order => 'trailpictures.id DESC')
Any help would be greatly appreciated!
Thank You,
Nick,
You need to order by created_at. :)
I finally figured it out. This is how it needs to be written:
#trails_pictures = Trail.find(:all,
:joins => 'INNER JOIN trailpictures ON trails.id = trailpictures.trail_id',
:limit => 20,
:conditions => ["trailpictures.parent_id is NULL"],
:select => 'trails.id, trails.name, trails.short_description, trails.city, trails.state, trails.country, max(trailpictures.id)',
:group => 'trails.id',
:order => 'max(trailpictures.id) DESC')
I need to generate a list of receipts in rails which need to be ordered by item's order relationship field (payment_method_meta_type.name).
Models :
Receipt
Deposit
PaymentMethodMetaType
In Deposit Model:
class Deposit < ActiveRecord::Base
belongs_to :payment_method_meta_type
has_many :receipts, :class_name=>"Receipt", :foreign_key=>"deposit_id",
:dependent => :destroy
end
I got an Array of Receipts in controller already :
#receipts = Receipt.find(:all, :conditions => ["date BETWEEN ? AND ?",
#start_date, #end_date], :order => "date DESC, id DESC",
:limit => limit, :offset => offset)
In the view I can show the payment_method_meta_type.name as well
- #receipts.each do |o|
%tr.
.....
%td #{o.receipt_number}
%td #{o.deposit.payment_method_meta_type.name}
.....
But how can I show the list by the order of receipts.deposit.payment_method_meta_type.name in the controller when I create the collection of the receipts array?
Try this:
#receipts = Receipt.all(:joins => {:deposit => :payment_method_meta_type},
:conditions => {:date => #start_date..#end_date},
:order => "payment_method_meta_types.name ASC",
:limit => limit, :offset => offset)
thx , I finally work it out, use 'include' in the query string
#receipts = Receipt.find(:all, :include => {:deposit => [:payment_method_meta_type] } ,:conditions => ["Receipts.business_date BETWEEN ? AND ?", #current_month_start_date, #current_month_end_date],:order => "tag_types.name , Receipts.business_date DESC",:limit => limit)