# Inner Join help

**URL:** <https://www.ruby-forum.com/t/inner-join-help/169737>\
**Category:** Rails\
**Created:** [June 8, 2009, 6:18am UTC](https://www.ruby-forum.com/t/inner-join-help/169737 "2009-06-08T06:18:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [June 8, 2009, 6:18am UTC](https://www.ruby-forum.com/t/inner-join-help/169737/1 "2009-06-08T06:18:53Z")

</div>

Hello!

I want to limit my model to just show companies from the current users  
account:

user.account.companies

I have this in my model which seems to be working:

def self.find(\*args)  
args \<\< {:conditions =\> XXXXX"}  
super  
end

Now I just need to turn this sql into rails conditions:

SELECT `companies`.\* FROM `companies` INNER JOIN `users` ON  
`companies`.user\_id = `users`.id WHERE ((`users`.account\_id = 1))

Can anyone help with this?

Thanks

Scott

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [June 8, 2009, 10:23am UTC](https://www.ruby-forum.com/t/inner-join-help/169737/2 "2009-06-08T10:23:20Z")

</div>

On Jun 8, 5:18 am, Scott H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> args << {:conditions => XXXXX"}
> super
> 
> ```
> 
> end

I would question the wisdom of doing that - it may work in the short  
term, but you may find it breaks other stuff.

> Now I just need to turn this sql into rails conditions:
> 
> SELECT `companies`.\* FROM `companies` INNER JOIN `users` ON  
> `companies`.user\_id = `users`.id WHERE ((`users`.account\_id = 1))

Take a look at the :joins option to find (if you have the associations  
right you don’t even need to write out the join clause in full,  
specifying the name of the association is enough).

In this case though I’m not sure why you wouldn’t just do  
some\_user.account.companies; you’d get back the same result set.

Fred

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [June 10, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/inner-join-help/169737/3 "2009-06-10T19:31:13Z")

</div>

> I would question the wisdom of doing that - it may work in the short  
> term, but you may find it breaks other stuff.

Thanks for the reply - I’ve just discovered default\_scope, which is  
exactly what I want. Now I just have to figure out how to do the inner  
join in rails.

> In this case though I’m not sure why you wouldn’t just do  
> some\_user.account.companies; you’d get back the same result set.

I have that association set up and it works fine. However, all the data  
in my app is associated to the current user/account. I want to set up  
the scope in one place and not have to worry about it every time i do a  
‘find’
