# Associations

**URL:** <https://www.ruby-forum.com/t/associations/106950>\
**Category:** Rails\
**Created:** [June 15, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/associations/106950 "2007-06-15T16:21:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 15, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/associations/106950/1 "2007-06-15T16:21:05Z")

</div>

Hello everybody,

I’ve got the following setup:

table request {  
id,  
site\_id,  
department\_id  
}

table site {  
id,  
site\_name  
}

table users {  
site\_id,  
user\_id,  
user\_department\_id  
}

So, my models are as follows:

# Model “request”

request belongs\_to :site

# Model “site”

has\_many :requests  
has\_many :users

# Model “users”

nothing interesting defined here (maybe there should be something?)

* * *

What I want to do is write a method in the model for “site” that  
would return the first user belonging to the department specified in  
“request”.

So, for now I wrote the method in model “site” like so:

def request\_department\_users(department\_id)  
users.each { |user| return user.id if user.department\_id ==  
department\_id)  
end

To use that function I would have to something like:  
Request.find(2).site.request\_department\_users(5)

The problem is that I have to pass department\_id as the parameter to  
the function. I’d like to somehow get the department\_id from  
“request” itself. Is it at all possible? Sorry for the long  
description. It is hard to explain.

Thanks,  
Sergei

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 15, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/associations/106950/2 "2007-06-15T17:43:56Z")

</div>

> # Model “users”
> 
> nothing interesting defined here (maybe there should be something?)

belongs\_to :site

> def request\_department\_users(department\_id)  
> users.each { |user| return user.id if user.department\_id ==  
> department\_id)  
> end
> 
> To use that function I would have to something like:  
> Request.find(2).site.request\_department\_users(5)

Try this instead of both the above:

request = Request.find(2)  
user = User.find(:first, :conditions =\> {:department\_id =\>  
request.department\_id})

steve

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 15, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/associations/106950/3 "2007-06-15T18:30:30Z")

</div>

I like it! So there’s no way to get to the request object out of either  
the  
site or users model?

Thanks, Steve.

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 15, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/associations/106950/4 "2007-06-15T18:45:19Z")

</div>

> So there’s no way to get to the request object out of either  
> the  
> site or users model?

Yes, since Site has\_many Requests

site = Site.find(:first)  
site.requests =\> an array of requests whose site\_id == site.id  
site.request\_ids =\> an array of request ids whose …  
site.request\_count =\> guess what 😉

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 15, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/associations/106950/5 "2007-06-15T19:24:44Z")

</div>

Steve, thanks once again. But I meant this: after I’ve created a new  
request  
and have a new request object, is there a way to access THAT particular  
object out of an instance of site or an instance of user since they are  
all  
connected with the model relationships? As I understand it, when I  
create a  
request object, the site and user objects are sort of also created and  
so  
there should be a way to go from user to site, to request, shouldn’t  
there?  
But I don’t mean through class methods, I mean through object methods.  
Does  
it make sense?

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 16, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/associations/106950/6 "2007-06-16T14:22:19Z")

</div>

Sergei Gerasenko wrote:

> Steve, thanks once again. But I meant this: after I’ve created a new  
> request  
> and have a new request object, is there a way to access THAT particular  
> object out of an instance of site or an instance of user since they are  
> all  
> connected with the model relationships?

Yes but you’d need to write a method in your site model like this:

def most\_recent\_request  
Request.find(self.request\_ids.sort.pop)  
end

You’d need to make sure the new request object was saved before you  
called this method because it doesn’t get allocated an id until it gets  
saved, and also that no other process has created a request object since  
the new one you have.

> As I understand it, when I  
> create a  
> request object, the site and user objects are sort of also created

That’s not correct. Although there is a defined relationship between the  
models individual objects are not related except by the mapping of their  
id to the foreign key in the other models. It’s quite possible to create  
a request object which has no related site, for example, by leaving the  
site\_id nil.

If I’ve misunderstood the question let me know.

steve

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [June 16, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/associations/106950/7 "2007-06-16T15:04:35Z")

</div>

Steve,

Thanks for a comprehensive reply. I agree with what you’re saying.  
Basically, since there’s no relationship between objects except for the  
foreign\_key/id kind, it would be impractical to find a request from  
either  
“site” or “users”. That makes sense and I’m glad you suggested the  
excellent  
workaround in your first reply. Thanks a bunch.

Sergei
