ruby on rails - Unable to call child methods in model method -


i'm having real struggle this:

in student.rb:

  def subject_avg     self.goals.each |goal|       goal.subject_id     end   end 

this doesn't 'do' - or should doesn't different from

  def subject_avg     self.goals.each |goal|       goal.id     end   end 

or

  def subject_avg     self.goals.each |goal|       goal.goal     end   end 

no matter what, returns array of goals belong subject:

[   #<goal id: 28, goal: "do on command", subject_id: 10, created_at: "2013-08-25 10:59:35", updated_at: "2013-08-25 10:59:35", default: nil>,    #<goal id: 29, goal: "make chunky", subject_id: 10, created_at: "2013-08-25 10:59:35", updated_at: "2013-08-25 10:59:35", default: nil>,    #<goal id: 30, goal: "hit mark", subject_id: 10, created_at: "2013-08-25 10:59:35", updated_at: "2013-08-25 10:59:35", default: nil>,    #<goal id: 31, goal: "puke , rally", subject_id: 10, created_at: "2013-08-25 10:59:35", updated_at: "2013-08-25 10:59:35", default: nil> ]  

at first thought wasn't reading/couldn't read each block reason passing result of self.goals.each (although seem what's happening). however, if call non-existent method, throws error:

  def subject_avg     self.goals.each |goal|       goal.ffs_do_something!     end   end 

returns

undefined method `ffs_do_something!' #<goal:0x000001064329f0> 

if put same each block in view, works expected (i can call methods on 'goal' within each block)

i think need map instead of each

  def subject_avg     self.goals.map |goal|       goal.subject_id     end   end 

or

  def subject_avg     self.goals.map(&:subject_id)   end 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -