I have a three models: locations > hotspots > accounts
In my locations model, I have this:
has_many :hotspots
has_many :accounts, :foreign_key => :calledstationidclean, :through => :hotspots
I’m trying to show some summary data from the accounts model for each location.
The following works ok:
scope :location_history, lambda { |id|
joins('INNER JOIN hotspots b ON locations.id = b.location_id INNER JOIN account ON b.mac = account.calledstationidclean')
.where(:id => id)
.select("count(locations.id) AS sessions_count, sum(account.acctoutputoctets) AS downloads, sum(account.acctinputoctets) AS uploads, count(distinct account.callingstationid) AS unique_user")
}
But I need to run some calculations and I can’t figure out how to do this.
Ordinarily, in the accounts model I’m able to do something like this:
def self.session_time
Account.sum("acctsessiontime")
end
def self.average_session
self.session_time / Account.count
end
How can I do something similar but in my location model – and display only the information for the specific location I’m viewing?? Do I actually need to run a join?
Yes, you will have to do the join.
By the way, you can use a group and combine all these with the records from location.
This will return Location objects with all the Location fields, and the aggregations you are doing.