In my application a user working at a dropzone can manipulate
transactions against customer accounts. Here’s my models:
class Transaction < ActiveRecord::Base
belongs_to :account
end
class Account < ActiveRecord::Base
belongs_to :dropzone
has_many :transactions
end
class Dropzone < ActiveRecord::Base
has_many :transactions, :through => :accounts do
def limited_to(transaction)
scope = Transaction.scoped({ :conditions =>
[‘account.dropzone_id = ?’, proxy_owner.id],
:joins => :account })
scope
end
end
end
In a controller, I’d like to write:
current_dropzone.transactions.limited_to(@transaction)
but, I get the error:
RuntimeError: ERROR C42P01 Mmissing FROM-clause entry for
table “account” P122 Fparse_relation.c L2017
RwarnAutoRange: SELECT “transactions”.* FROM “transactions” INNER
JOIN “accounts” ON “accounts”.id = “transactions”.account_id WHERE
(account.dropzone_id = 2) (ActionView::TemplateError)
I’ve searched all over and tried many options but can’t figure this
out, I hope someone can help.
Many thanks,
Bryan
Hi –
On Fri, 28 Aug 2009, Bryan A. wrote:
has_many :transactions
end
class Dropzone < ActiveRecord::Base
has_many :transactions, :through => :accounts do
def limited_to(transaction)
scope = Transaction.scoped({ :conditions =>
[‘account.dropzone_id = ?’, proxy_owner.id],
That should be accounts (plural) I suspect.
David
–
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (The Well-Grounded Rubyist)
September Ruby training in NJ has been POSTPONED. Details to follow.
David,
thank you so much, that was the correct answer.
My complete association extension is now:
class Dropzone < ActiveRecord::Base
has_many :transactions, :through => :accounts do
def limited_to(transaction)
scope = Transaction.scoped({ :conditions =>
[‘accounts.dropzone_id = ?’, proxy_owner.id],
:include =>
[:account, :payment_method, :slot] })
scope = scope.scoped :conditions => [‘name ILIKE ?’, “%#
{transaction.account_name}%”] unless transaction.account_name.blank?
scope = scope.scoped :conditions => [‘payment_method_id = ?’,
transaction.payment_method_id] unless
transaction.payment_method_id.blank?
scope
end
end
end