Rails 3.1.3 I have tables User => has_many :contribution Contribution => belongs_to :user in user.rb, I would like to define a method to compute the total contribution (sum of field ''price:integer'' ) def total_contribution @contributions = Contribution.find_all_by_user(self) total = 0 @contributions.each do |c| if c.done total += c.price end end return total end "Contribution.find_all_by(self)" gives an error undefined method `find_all_by'' for Is there any better way? soichi -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 30 April 2012 09:03, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Rails 3.1.3 > > I have tables > > User => has_many :contributionThat should be :contributions, plural.> Contribution => belongs_to :user > > in user.rb, I would like to define a method to compute the total > contribution > (sum of field ''price:integer'' ) > > def total_contribution > @contributions = Contribution.find_all_by_user(self)Just use @contributions = self.contributions Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for your answer. but it gives a different error SQLite3::SQLException: no such column: contributions.user_id: SELECT "contributions".* FROM "contributions" WHERE "contributions"."user_id" = 3 Maybe the association is not properly set up? soichi -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 30 April 2012 09:24, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: Please don''t top post, it makes it difficult to follow the thread. Insert your reply at appropriate point(s) in previous message. Thanks> Thanks for your answer. but it gives a different error > > SQLite3::SQLException: no such column: contributions.user_id: SELECT > "contributions".* FROM "contributions" WHERE "contributions"."user_id" > = 3 > > Maybe the association is not properly set up?Is there a column user_id in the contributions table? You should have added one for the the belongs_to association. I think it would be worth your while working right through some tutorials on Rails in order better to understand the basics of Rails. railstutorial.org is good and free to use online. Colin> > soichi > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Please don''t top post, it makes it difficult to follow the thread. > Insert your reply at appropriate point(s) in previous message.Sorry about that. I will be careful.> > Is there a column user_id in the contributions table? You should have > added one for the the belongs_to association.I was thinking of id of User table (created by Devise) rather than the ''user_id'' which are supposed to be attached to ''Contribution'' table. Stupid misunderstanding of mine ;)> tutorials on Rails in order better to understand the basics of Rails. > railstutorial.org is good and free to use online.Thanks for the info. soichi -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.