Problem with my associations Users -> Accounts -> Transactions

41 views
Skip to first unread message

Bizt

unread,
Jan 31, 2014, 8:31:41 PM1/31/14
to rubyonra...@googlegroups.com
I have three tables: users, accounts, and transactions. Users have many accounts, accounts have many transactions. So I define my models like so:

class User < ActiveRecord::Base
  has_many :accounts, dependent: :destroy
end

class Account < ActiveRecord::Base
  belongs_to :user
  has_many :transactions, dependent: :destroy
end

class Transaction < ActiveRecord::Base
  belongs_to :account
end

Ideally on my app. When the current_user (btw, I'm also using Devise ;) clicks shows accounts, then clicks an account to show transactions for that page - eventually I end up on /transactions?account=2 URL. Until here works fine.

In my transactions controller, however, I was to display on that page transactions for that user (current_user) for that account (id=2). I'm not sure the correct way to do this thought. Below is what I was trying:

def index
  @account = current_user.accounts.find_by_id(params[:accoount])
  @transactions = @account.transactions.find_all_by_account(params[:accoount]);
end

..but it doesn't work :( Where am I going wrong?

Bizt

unread,
Jan 31, 2014, 10:59:46 PM1/31/14
to rubyonra...@googlegroups.com
Btw, I get the following error:

undefined method `transactions' for nil:NilClass


To me this tells me that @account if not found, or is returning an empty array. Am I using the wrong method, or parameters?

vikram jain

unread,
Jan 31, 2014, 11:29:50 PM1/31/14
to rubyonra...@googlegroups.com
Please, try Users instead of current_user, like :
def index 
  @account = Users.accounts.find_by_id(params[:accoount])

  @transactions = @account.transactions.find_all_by_account(params[:accoount]);
end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fd67df38-8b15-46ae-9f72-2c94068c12fb%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Thanks & Regard
   Vikram Jain
=============

Bizt

unread,
Feb 1, 2014, 12:45:25 AM2/1/14
to rubyonra...@googlegroups.com
Aaargh!!! I see it, I didn't type "account" correctly. I thought I was using the method incorrectly. How did I not see that for so long?!


@account = Users.accounts.find_by_id(params[:accoount])


This now works - @account = current_user.accounts.find_by_id(params[:account])

Bizt

unread,
Feb 1, 2014, 12:47:30 AM2/1/14
to rubyonra...@googlegroups.com
Actually I've changed the whole thing to:

def index

  @account = current_user.accounts.find_by_id(params[:account])
  @transactions = @account.transactions;
end

.. works fine

Colin Law

unread,
Feb 1, 2014, 4:44:30 AM2/1/14
to rubyonra...@googlegroups.com
On 1 February 2014 05:47, Bizt <marty...@gmail.com> wrote:
> Actually I've changed the whole thing to:
>
> def index
>
> @account = current_user.accounts.find_by_id(params[:account])
> @transactions = @account.transactions;
> end
>

@account = Account.find(params[:account])
should also work. A possible disadvantage of this method (depending
on your application) is that a manually constructed url could show an
account that does not belong to the current_user.

Colin

Hassan Schroeder

unread,
Feb 1, 2014, 11:54:24 AM2/1/14
to rubyonrails-talk
On Fri, Jan 31, 2014 at 9:47 PM, Bizt <marty...@gmail.com> wrote:

> def index
> @account = current_user.accounts.find_by_id(params[:account])
> @transactions = @account.transactions;
> end
>
> .. works fine

Consider, though, that if for whatever reason an invalid id is passed
in, @account will be nil and so @account.transactions will give you
a NoMethod error.

What do you want to happen at that point?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Bizt

unread,
Feb 1, 2014, 8:15:38 PM2/1/14
to rubyonra...@googlegroups.com


Consider, though, that if for whatever reason an invalid id is passed
in, @account will be nil and so @account.transactions will give you
a NoMethod error.

What do you want to happen at that point?


Good point. Thanks, I'll put some checking to see if account is valid or not and handle it accordingly.

Matt Jones

unread,
Feb 3, 2014, 7:42:53 AM2/3/14
to rubyonra...@googlegroups.com
One quick way to do that is to use plain `find` instead of `find_by_id`, thus:

@account = current_user.accounts.find(params[:account])

If a user attempts to request an account that doesn't belong to them, this will raise ActiveRecord::RecordNotFound. The default Rails rescue_from will translate that into an HTTP 404 Not Found response.

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages