Associations

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

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

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

Thanks, Steve.

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 :wink:

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?

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs