Received: by 10.50.76.133 with SMTP id k5mr9284918igw.2.1335773745123; Mon, 30 Apr 2012 01:15:45 -0700 (PDT) X-BeenThere: rubyonrails-talk@googlegroups.com Received: by 10.231.83.209 with SMTP id g17ls3319714ibl.2.gmail; Mon, 30 Apr 2012 01:14:45 -0700 (PDT) Received: by 10.50.155.137 with SMTP id vw9mr9285134igb.3.1335773685833; Mon, 30 Apr 2012 01:14:45 -0700 (PDT) Received: by 10.50.155.137 with SMTP id vw9mr9285132igb.3.1335773685818; Mon, 30 Apr 2012 01:14:45 -0700 (PDT) Return-Path: Received: from mail-ob0-f172.google.com (mail-ob0-f172.google.com [209.85.214.172]) by gmr-mx.google.com with ESMTPS id no9si6047482igc.0.2012.04.30.01.14.45 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 30 Apr 2012 01:14:45 -0700 (PDT) Received-SPF: pass (google.com: domain of clan...@googlemail.com designates 209.85.214.172 as permitted sender) client-ip=209.85.214.172; Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of clan...@googlemail.com designates 209.85.214.172 as permitted sender) smtp.mail=clan...@googlemail.com; dkim=pass header...@googlemail.com Received: by obbeh20 with SMTP id eh20so525072obb.3 for ; Mon, 30 Apr 2012 01:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=TPifeGD9FQIUT73QYY2NB3zTM4euI7zg1Kbz2Wz0/84=; b=oNWaoaEg/RhFCy9pZnU5S6WKTQL7JE7TWiFMye6VmpFeSnZBvKEUuuU39wpFIRsfIp d8TDyKPydDQBEudfboY8ypmPuUMujzF7PZ6lRSeHifPBBVCdpjsY3CUR66OzTaiiA9L/ cI+3wSB4WSWJJVOxlay/tzq4QOjIxbmwnrYXHcTxl+iHzfXu0UIReg/dRubax0LA/EA0 eae5W9ikFAP3cJqs+9ZE3XcurwK/7mYVYR5ZlbRSy9SnqrGpadWTtVBGGHnaXz7efiJw TRlYxv5znQ94EpnSLchhG3rQxUz7KB3Gyc7xx5QuCXi7K8JhwQZ2f13fujomlXJPG0mi sHOA== Received: by 10.182.183.73 with SMTP id ek9mr1737130obc.15.1335773685543; Mon, 30 Apr 2012 01:14:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.182.165.105 with HTTP; Mon, 30 Apr 2012 01:14:15 -0700 (PDT) In-Reply-To: <314a41cc2ff94e83605c9378b44e8...@ruby-forum.com> References: <314a41cc2ff94e83605c9378b44e8...@ruby-forum.com> From: Colin Law Date: Mon, 30 Apr 2012 09:14:15 +0100 Message-ID: Subject: Re: [Rails] find_all_by.association To: rubyonrails-talk@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 30 April 2012 09:03, Soichi Ishida wrote: > Rails 3.1.3 > > I have tables > > User =C2=A0=3D> has_many :contribution That should be :contributions, plural. > Contribution =3D> belongs_to :user > > in user.rb, I would like to define a method to compute the total > contribution > (sum of field 'price:integer' ) > > =C2=A0def total_contribution > =C2=A0 =C2=A0@contributions =3D Contribution.find_all_by_user(self) Just use @contributions =3D self.contributions Colin