# Model conditions

**URL:** <https://www.ruby-forum.com/t/model-conditions/140375>\
**Category:** Rails\
**Created:** [June 13, 2008, 11:41am UTC](https://www.ruby-forum.com/t/model-conditions/140375 "2008-06-13T11:41:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 11:41am UTC](https://www.ruby-forum.com/t/model-conditions/140375/1 "2008-06-13T11:41:38Z")

</div>

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:

```auto
  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:

```auto
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

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/model-conditions/140375/2 "2008-06-13T12:20:32Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/model-conditions/140375/3 "2008-06-13T12:37:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 11:55am UTC](https://www.ruby-forum.com/t/model-conditions/140375/4 "2008-06-13T11:55:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/model-conditions/140375/5 "2008-06-13T13:39:00Z")

</div>

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.

```auto
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.

```auto
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

```auto
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)

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/model-conditions/140375/6 "2008-06-13T14:13:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [June 13, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/model-conditions/140375/7 "2008-06-13T17:33:38Z")

</div>

Nice observation! 🙂

- 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
