find in a nested object

36 views
Skip to first unread message

michalyad

unread,
Feb 22, 2011, 8:50:04 AM2/22/11
to Israel.rb - the Israel Ruby & Israel Rails Developers group
I have an object parent with nested children.

I read the parent from the data base by:
@parent=Parent.find_by_name("dani")

Thus I have all dani's children in @parent.

Now I would like to find "moshe" and update it.

When I write: @parent.children.find_by_name("moshe") it does a select
on the database although I shall have it memory.

When I wrtie a loop: @parent.children.each do.... then it does not go
to the database.

How can I do a find that will not do a select?

Dimitri Krassovski

unread,
Feb 22, 2011, 8:58:22 AM2/22/11
to isra...@googlegroups.com
A association in rails pretends to be a array when loaded, so calling array method on it works, but find_by_name is not a array method, so it's called on the association proxy instead, which doesn't know better than go to the db and do a conditional find.

> --
> You received this message because you are subscribed to the Google Groups "Israel.rb - the Israel Ruby & Israel Rails Developers group" group.
> To post to this group, send email to isra...@googlegroups.com.
> To unsubscribe from this group, send email to israelrb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
>

--
Dimitri Krassovski

Elad Meidar

unread,
Feb 22, 2011, 9:02:03 AM2/22/11
to isra...@googlegroups.com
parent = Parent.find_by_name("Elad", :include => :children)

Elad Meidar

Michal Yad-Shalom

unread,
Feb 22, 2011, 9:12:00 AM2/22/11
to isra...@googlegroups.com
I would like to find a child with name "moshe" not a parent.
--
Michal Yad Shalom
054-4315617

Dimitri Krassovski

unread,
Feb 22, 2011, 9:13:20 AM2/22/11
to isra...@googlegroups.com
Do you only need the child?
Just do Child.find_by_name_and_parent_id
-- 
Dimitri Krassovski

Michal Yad-Shalom

unread,
Feb 22, 2011, 9:18:48 AM2/22/11
to isra...@googlegroups.com
I have already read the parent and all his children from the database. Now instead of writing a loop (@parent.children.each do...) to find a certain child, I would like to write a single find command that will not go to the database. 

Can I do this?

Dimitri Krassovski

unread,
Feb 22, 2011, 9:23:07 AM2/22/11
to isra...@googlegroups.com
@moshe = @parent.children.select{|c| c.name == "moshe"}.first
This should do it for you.

And, forgive me for my rudeness, go read a Ruby book when you're done.

Elad Meidar

unread,
Feb 22, 2011, 10:00:40 AM2/22/11
to isra...@googlegroups.com
and will raise an exception if there are no matches.

by using eager loading (either :include or :joins options) she'll be able to use Association#find without queries being made
Elad Meidar,
CEO, Nautilus6 Inc.
http://blog.eizesus.com
http://www.nautilus6.com

Michal Yad-Shalom

unread,
Feb 22, 2011, 9:55:54 AM2/22/11
to isra...@googlegroups.com
Dimitri Thanks.

BTW... your "rudeness" was in place.  
Reply all
Reply to author
Forward
0 new messages