Created
February 20, 2013 17:16
-
-
Save brettnem/4997225 to your computer and use it in GitHub Desktop.
Using activerecord without rails issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CDR < ActiveRecord::Base | |
establish_connection "data1" | |
set_table_name "cdrs" | |
end | |
## The following fails: | |
agg_data = CDR.group(:account_id).select('account_id,SUM(billable)/60 as minutes,SUM(total_retail) as total_retail').where("datestamp = ?", date.to_s) | |
## ERROR: /usr/lib/ruby/vendor_ruby/active_record/base.rb:1998:in `method_missing' SCHEMA (0.8ms) SHOW TABLES | |
: undefined method `group' for #<Class:0x7f8787b77c60> | |
--------------------------------------------- | |
## This also fails: | |
test = CDR.where(account_id='1234') | |
## ERROR: /usr/lib/ruby/vendor_ruby/active_record/base.rb:1998:in `method_missing' SCHEMA (1.6ms) SHOW TABLES | |
: undefined method `where' for #<Class:0x7f6558383eb0> (NoMethodError) | |
--------------------------------------------- | |
## This works fine: | |
agg_data = CDR.find(:all,:select => [:account_id,",SUM(billable)/60 as xxzzy,SUM(total_retail) as aabbbc"], :group => 'account_id', :conditions => ['datestamp = ?',date.to_s]) | |
But agg_data only includes the account_id column, xxzzy and aabbbc are missing entirely: | |
(output of agg_data.inspect) | |
#<CDR account_id: 11252> | |
#<CDR account_id: 11218> | |
#<CDR account_id: 11360> | |
#<CDR account_id: 11283> | |
#<CDR account_id: 11105> | |
#<CDR account_id: 11142> | |
So obviously it's returning a CDR object that doesn't have attr_attributes included for the sums. What's the right way to create a new object (not a CDR object) that includes the aggregates? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment