Forum: Ruby on Rails Rails Relationships

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
160697aa31c644cfa2cce78e3a0f613d?d=identicon&s=25 Neal (Guest)
on 2005-12-12 17:13
I'm trying to find an elegant solution to a data model problem and was
wondering if some of you good people could help.  I'm setting up a
simple book-loan tracking system, where an office has many people and
people have many books.  i.e.:

office --> (has many) people --> (has many) books

while office.people returns a list of people belonging to an office and
people.books returns a list of books belonging to a person, is there a
function such as  office.books  that would return all of the books
belonging to all of the people in the office?

Thanks,
-Neal
03cb7371db223067d0daab54e1f95cb2?d=identicon&s=25 Abdur-Rahman Advany (Guest)
on 2005-12-12 17:18
(Received via mailing list)
I don't think thats build in, but you could very easily define a method
in office (books).
E5addfb886fa24edf1fcf9473a9b5a34?d=identicon&s=25 jmaziarz <jeremy.maziarz@gmail.com> <jeremy.maziar (Guest)
on 2005-12-12 17:26
Neal wrote:
> I'm trying to find an elegant solution to a data model problem and was
> wondering if some of you good people could help.  I'm setting up a
> simple book-loan tracking system, where an office has many people and
> people have many books.  i.e.:
>
> office --> (has many) people --> (has many) books
>
> while office.people returns a list of people belonging to an office and
> people.books returns a list of books belonging to a person, is there a
> function such as  office.books  that would return all of the books
> belonging to all of the people in the office?
>
> Thanks,
> -Neal

This may not be the most eligant solution but I would add a method to
your 'book' model similar to this:

def self.find_all_by_office_id(office_id)
    book_collections = Person.find_all_by_office_id(office_id).collect {
|p| p.books }
    book_collections.flatten!
end

In a controller I would then use:

books_by_office = Book.find_all_by_office_id(office.id)

Hope this helps a bit.
9a5beb2c3d87189f401188395e6c4032?d=identicon&s=25 Juraci Krohling Costa (Guest)
on 2005-12-12 17:30
(Received via mailing list)
def books
  Book.find(:all, :conditions=>["person_id IN (?)",
people.collect{|person| person.id}.join(", ")] ) ## not tested
end
160697aa31c644cfa2cce78e3a0f613d?d=identicon&s=25 Neal <nlober@lobersystems.com> (Guest)
on 2005-12-12 18:12
Juraci Krohling Costa wrote:
> def books
>   Book.find(:all, :conditions=>["person_id IN (?)",
> people.collect{|person| person.id}.join(", ")] ) ## not tested
> end

That function works great! Thanks!

How exactly is it joining the tables, though???

Thanks
9a5beb2c3d87189f401188395e6c4032?d=identicon&s=25 Juraci Krohling Costa (Guest)
on 2005-12-12 18:27
(Received via mailing list)
Er.. its not joining... You cannot use :include in two level's in
ActiveRecord, but, I think the last thing you want to do w/ this method
is something like this: office.books.people.books :-)
If you really need to join books w/ people in this method, uses this:

def books

  Book.find(:all, :include=>:person, :conditions=>["person_id IN (?)",
people.collect{|person| person.id}.join(", ")] ) ## you told me this
works =D

end
17acfbd3a05cfb59e0543235cc0a813b?d=identicon&s=25 joshua (Guest)
on 2005-12-12 18:32
Neal <nlober@lobersystems.com> wrote:
> Juraci Krohling Costa wrote:
>> def books
>>   Book.find(:all, :conditions=>["person_id IN (?)",
>> people.collect{|person| person.id}.join(", ")] ) ## not tested
>> end
>
> That function works great! Thanks!
>
> How exactly is it joining the tables, though???

The "join" message is not an SQL join operation. It is a collection
method that joins all the items in a collection into a string, separated
by in this case a comma.  What that code does is fetches all the books
that belong to any person in that office by filtering on the person_id
value of the book, where the people collection equals office.people.
There is of course a way to do that all in SQL - you'll have to decide
which approach is better for your uses.
8b4ee2d4c81b34a644c1b0f5606ed5ee?d=identicon&s=25 Santiago Erquicia (Guest)
on 2005-12-13 12:16
(Received via mailing list)
On 12/12/05, Juraci Krohling Costa <juca@jkcosta.info> wrote:
> def books
>   Book.find(:all, :conditions=>["person_id IN (?)",
> people.collect{|person| person.id}.join(", ")] ) ## not tested
> end
>


I'm trying to use this method but I have a problem.  This inserts
everything like a string example .... IN ('1,3')

Is there any way to avoid rails to add those quotes?

Thanks,
Santiago
8b4ee2d4c81b34a644c1b0f5606ed5ee?d=identicon&s=25 Santiago Erquicia (Guest)
on 2005-12-13 12:31
(Received via mailing list)
On 12/13/05, Santiago Erquicia <santiago.erquicia@gmail.com> wrote:
>
> Is there any way to avoid rails to add those quotes?
>
> Thanks,
> Santiago
>

OK, I solved this by myself.

Book.find(:all, :conditions=>"person_id IN #{people.collect{|person|
person.id}.join(', ')}")

Santiago
This topic is locked and can not be replied to.