Ruby on Rails Friday, January 31, 2014

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?

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d68efa8e-dc09-4bb1-b84f-8ab71888bfe8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment