belong to and search

If you have set up the associations (belongs_to and has_many) then if you have a Child object child you can just say child.mother to get the mother. Similarly if you have a mother object you can say mother.children to get the children. It is all done by magic.
Colin

Hi

thanks works like a charm!

but i'm just curious performance wise
when I look at mongrel it seems that there are two queries
first the children.all()
then for each record it does a new query for mother get name...

now is that as quick compare to juts one query with a left join and get
all the data in children().

Colin Law wrote:

Hi

thanks works like a charm!

but i’m just curious performance wise

when I look at mongrel it seems that there are two queries

first the children.all()

then for each record it does a new query for mother get name…

now is that as quick compare to juts one query with a left join and get

all the data in children().

If you know you are going to be accessing the associated objects use :include in the find to tell Rails to fetch all the data in one query, so something like
@children = Child.find(:all, :include => :mother)

By the way, I think the class should be Child, the table children and the controller children_controller.
Rails should know that children is the plural of child.

If you are really building a database of family relationships then you might want to search for a thread here a few weeks ago about how to organise the db. There were some very interesting ideas discussed.

Colin

Hi thank you

so if I use the include
how do i just retrieve the name then

example this is using the "magic way"

@children do |child|

  ... print child.mother.name

end

Hi thank you

so if I use the include

how do i just retrieve the name then

Do you mean you want a query finding the child, including the mother.name but not the other mother fields? I do not know whether you can do that. I think the overhead of fetching all columns is going to be rather small.

Colin

If you want to retrieve just the name, than use the :select option

with your find:

@children = Child.find(:all, :include => :mother, :select => ‘name’)

Does that select child.name or mother.name?
Colin

Yeah I deleted that post immediately after because that selects
child.name. Something like :select => "mothers.name AS mother_name"
would probably work, but just ugly AND then you'd need a mother_name
virtual attribute in the Child model, making things rather needlessly
horrific. I really can't think of a good way (without raw SQL) to do
this in one query either.

OK I'm dumb; you definitely don't need a virtual attribute for that
(not really sure what i was thinking), but its still ugly probably not
worth doing.