mysql - Mysql2::Error: Unknown column 'sum_hours' in 'field list' -


its been 2 months since started learn rails doing modifications on plugin of redmine in enterprise (as student learning way of "life"), problem cant find solution anywhere.

let me explain:

i needed group huge ammount of time entries user_id , project_id(no problems that) if need sum hours in groups.

the estructure this:

| id | project_id | user_id | issue_id | hours |

and information built method :

def time_entries_for_user(user, options={})     extra_conditions = options.delete(:conditions)     return timeentry.select('*,sum(hours) sum_hours').       includes(self.includes).       where(self.conditions([user], extra_conditions)).       group('issue_id, time_entries.project_id').       order('issues.id asc') end 

i added select because needed column plus sum 1 , group alone works , cant make sum columns appear. json code result of method:

{"test1 / subproyectotest1":{"logs":[{"id":24,"project_id":4,"user_id":1,"issue_id":10,"hours":6.0,"comments":"","activity_id":8,"spent_on":"2016-05-03","tyear":2016,"tmonth":5,"tweek":18,"created_on":"2016-05-03t11:07:09.000z","updated_on":"2016-05-03t11:07:09.000z","sum_hours":9.0}],"users":[{"id":1,..... 

the funny thing in other method groups project , sum column appear. 1 works :

def time_entries_for_all_users(project)     return project.time_entries.select('*,sum(hours) sum_hours').     includes(self.includes).     where(self.conditions(self.users)).     group('issue_id,user_id')       order('issues.id asc')     end   {"test1 / subproyectotest1":{"logs":[{"id":24,"project_id":4,"user_id":1,"issue_id":10,"hours":6.0,"comments":"","activity_id":8,"spent_on":"2016-05-03","tyear":2016,"tmonth":5,"tweek":18,"created_on":"2016-05-03t11:07:09.000z","updated_on":"2016-05-03t11:07:09.000z","sum_hours":9.0}],"users":[{"id":1....... 

in logs, error shows :

actionview::template::error (undefined method `sum_hours' #)

referring code on view renders time entries:

 <% when l(:field_hours) %> <strong><%= number_with_precision(time_entry.sum_hours, :precision => @precision) %></strong> 

well, cant call doesn't exists...

2ยบ time asking in stackoverflow if need more info because forgot write , :) .

regards , thanks.

edited:

thanks borama , emiliano, work adding relation manually. code of first sample changed :

    def time_entries_for_user(user, options={})         extra_conditions = options.delete(:conditions)          return timeentry.select('*,sum(hours) sum_hours').               includes(self.includes).               where(self.conditions([user], extra_conditions)).               where("time_entries.user_id",user)               group('issue_id, time_entries.project_id').               order('issues.id asc')           end    end 

though show 1 record proper sum of hours but,im trying group issue_id , groups user_id causing show 1 record.

edited 2: finally!! didnt realize order of methods can alter result :/. final code :

  def time_entries_for_user(user, options={})     extra_conditions = options.delete(:conditions)      return timeentry.group('time_entries.project_id,time_entries.issue_id').       select('*,sum(hours) sum_hours').       includes(self.includes).       where(self.conditions([user], extra_conditions)).       where("time_entries.user_id",user)       order('issues.id asc')   end 

thank guys!

in tests, manually selected column (sum_hours in case), accessible "virtual" attribute when execute query on model, per first example - time_entries_for_user method.

however, not seem work if try add custom select association of record. exact same error (a nomethoderror). start working, i'd advise rewrite second query (the time_entries_for_all_users) form without using associations, i.e. this:

def time_entries_for_all_users(project)   timeentry.select('*,sum(hours) sum_hours').     # <- no association used here       includes(self.includes).       where(self.conditions(self.users)).       where("time_entries.project_id", project.id).  # <- assoc. added here instead       group('issue_id,user_id')       order('issues.id asc') end 

nothe instead of calling time_entries association on current project record, selecting time entries having project id. should return same records original query custom columns should work correctly. you'll need update query further other conditions , includes work ok too.

as second option, can define association manual select:

has_many :time_entries_with_hours_sum,            -> { select("time_entries.*, sum(hours) sum_hours") }, class_name: "timeentry" 

then custom column should accessible using new association:

project.time_entries_with_hours_sum.first.sum_hours # => 9.0 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -