Model conditions

Hi.

I got a model for banks accounts, each account has many bank_account_transactions. Each transaction has a from_account_id and a to_account_id. I'm trying to find all transactions both to and from the account with a has_many-relation and a conditions-clause.

The account model looks like this:

[code]   has_many :transactions,     :class_name => "BankAccountTransaction",     :conditions => "to_account_id = #{id} or from_account_id = #{id}" [/code]

When i try to call the relation trough @account.transactions i get this error:

[code] SQLite3::SQLException: no such column: bank_account_transactions.bank_account_id: SELECT * FROM "bank_account_transactions" WHERE ("bank_account_transactions".bank_account_id = 1 AND (to_account_id = 23229850 or from_account_id = 23229850)) ORDER BY created_at desc [/code]

The problem here is, that the relation keeps caling the transaction.bank_account_id, and the model does not have one. How do i remove the normal foreign_key from the relation?

- Emil

Not sure if that works, but instead of using :conditions you could use :finder_sql => ... That should tell rails to ignore the default foreign_key

I have tried this, but I can't seem to get the account-id into the sql. If I run this:

[code] has_many :transactions,   :class_name => "BankAccountTransaction",   :finder_sql => "select * from bank_account_transactions where bank_account_transactions.to_account_id = #{id} or bank_account_transactions.from_account_id = #{id}" [/code]

Then I get an id of a couple of thousands, I suspect it is some internal id, because, it is definitely not the account id. How do I get the account id?

Thorsten Mueller wrote:

Then I get an id of a couple of thousands, I suspect it is some internal id, because, it is definitely not the account id. How do I get the account id?

try self.id or self[:id] or read_attribute(:id)

Firstly, thank you for the quick and very good answers. I tried them all, but with the following results.

This gave an unrealistic hight id-number, that is probably the internal id again. [code] has_many :transactions,   :class_name => "BankAccountTransaction",   :finder_sql => "select * from bank_account_transactions where bank_account_transactions.to_account_id = #{self.id} or bank_account_transactions.from_account_id = #{self.id}" [/code]

This results in an error: expected an array, got nil. [code] has_many :transactions,   :class_name => "BankAccountTransaction",   :finder_sql => "select * from bank_account_transactions where bank_account_transactions.to_account_id = #{self[:id]} or bank_account_transactions.from_account_id = #{self[:id]}" [/code]

Results in undefined method [code] has_many :transactions,   :class_name => "BankAccountTransaction",   :finder_sql => "select * from bank_account_transactions where bank_account_transactions.to_account_id = #{read_attribute(:id)} or bank_account_transactions.from_account_id = #{read_attribute(:id)}" [/code]

So i really cant seem to find a solution. Do you by any chance have any other ideas?

Thorsten Mueller wrote:

Firstly, thank you for the quick and very good answers. I tried them all, but with the following results.

You need to use single quotes if you want the condition/finder sql to
be interpolated at association load time rather than at class load time

Fred

Nice observation! :slight_smile:

- Emil

Frederick Cheung wrote: