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:

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

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

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

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

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

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}"

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 M. wrote:

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

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)

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

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.

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}"

This results in an error: expected an array, got nil.

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]}"

Results in undefined method

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)}"

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

Thorsten M. 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)

On 13 Jun 2008, at 12:39, Emil K. 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 C. wrote:

On 13 Jun 2008, at 12:39, Emil K. 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