mysql - Error undefined local variable or method `created_at' -


i write query find out how user has reduced calories in week, have error.

how avoid mistakes?

def self.calories_burned(current_user)   week = ((created_at - current_user.first_program_started_at.utc.beginning_of_day) / 86400 / 7).ceil.to_i || 1   find_by_sql("     select        count(*) cnt,        week(#{week}) week_number            user_daily_updates             user_id=#{current_user.id}      group        week_number   ") end 

when write this:

def self.calories_burned(current_user)     etc... end 

it's method can called this:

myusermodel.calories_burned(some_user)

in case running code on class before instantiated, means model hasn't attached connection database , because of not able access attributes pertain model.

on other hand, if write this:

def calories_burned     etc... end 

you don't need pass user method, call on controller after instantiating model, this:

id = 123 current_user = myusermodel.find(id) current_user.calories_burned 

where current_user.calories_burned return value looking based on current user.

after taking closer @ method

it should more this:

def calories_burned     week = ((created_at - first_program_started_at.utc.beginning_of_day) / 86400 / 7).ceil.to_i || 1     userdailyupdate.where(["user_id = ?", id]).where(["week = ?",week]]).count(:all, :group => 'week_number') end 

if understood correctly trying query, should give same result. now, should mention assuming when created table user_daily_updates, created model userdailyupdate.

you should take @ activerecord's documentation searching using conditions (all of section 2) in order have better understanding of did.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -