# Query problem

**URL:** <https://www.ruby-forum.com/t/query-problem/127906>\
**Category:** Rails\
**Created:** [February 2, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/query-problem/127906 "2008-02-02T14:38:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 2, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/query-problem/127906/1 "2008-02-02T14:38:46Z")

</div>

Hi all,

I have users (has many) accounts

I want to show all users and the account (if he has one) of a certain  
type.

How can I query all users and if the user has a specific account, the  
account. If I do :conditions =\> :accounts.type = ‘type’ I only get the  
users with an account. I also want the users with an account.

Someone has an idea how to solve this?

Thanks  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 2, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/query-problem/127906/2 "2008-02-02T15:44:00Z")

</div>

Tarscher wrote:

> I have users (has many) accounts
> 
> I want to show all users and the account (if he has one) of a certain  
> type.
> 
> How can I query all users and if the user has a specific account, the  
> account. If I do :conditions =\> :accounts.type = ‘type’ I only get the  
> users with an account. I also want the users with an account.

Sounds like you want to do the account selection in Rails after the  
fact. Something like:

Users.find(:all).each do |user|  
user.show  
account = user.account  
account.show if account && account.type == whaka\_whaka  
end

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 3, 2008, 1:21am UTC](https://www.ruby-forum.com/t/query-problem/127906/3 "2008-02-03T01:21:53Z")

</div>

Tarscher wrote:

> Hi all,
> 
> I have users (has many) accounts
> 
> I want to show all users and the account (if he has one) of a certain  
> type.
> 
> How can I query all users and if the user has a specific account, the  
> account. If I do :conditions =\> :accounts.type = ‘type’ I only get the  
> users with an account. I also want the users with an account.
> 
> Someone has an idea how to solve this?
> 
> Thanks  
> Stijn

Please read the last line that you wrote and read it back to yourself  
and see if it makes any sense to you…  
“If i do … I only get the users with and account, I  
also want the users with an account” If you rephrase, I think we will  
be able to serve you better…

Also, please submit the associations exactly as you have them specified  
in your models…

ilan
