Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
find in a nested object
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
michalyad  
View profile   Translate to Translated (View Original)
 More options Feb 22 2011, 8:50 am
From: michalyad <michal...@gmail.com>
Date: Tue, 22 Feb 2011 05:50:04 -0800 (PST)
Local: Tues, Feb 22 2011 8:50 am
Subject: find in a nested object
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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dimitri Krassovski  
View profile  
 More options Feb 22 2011, 8:58 am
From: Dimitri Krassovski <lab...@startika.com>
Date: Tue, 22 Feb 2011 15:58:22 +0200
Local: Tues, Feb 22 2011 8:58 am
Subject: Re: find in a nested object
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.

On Feb 22, 2011, at 15:50 , michalyad wrote:

--
Dimitri Krassovski

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Elad Meidar  
View profile  
 More options Feb 22 2011, 9:02 am
From: Elad Meidar <eize....@gmail.com>
Date: Tue, 22 Feb 2011 16:02:03 +0200
Local: Tues, Feb 22 2011 9:02 am
Subject: Re: find in a nested object

parent = Parent.find_by_name("Elad", :include => :children)

Elad Meidar
http://blog.eizesus.com
http://twitter.com/eladmeidar

On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michal Yad-Shalom  
View profile  
 More options Feb 22 2011, 9:12 am
From: Michal Yad-Shalom <michal...@gmail.com>
Date: Tue, 22 Feb 2011 16:12:00 +0200
Subject: Re: find in a nested object

I would like to find a child with name "moshe" not a parent.

--
Michal Yad Shalom
054-4315617

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dimitri Krassovski  
View profile  
 More options Feb 22 2011, 9:13 am
From: Dimitri Krassovski <lab...@startika.com>
Date: Tue, 22 Feb 2011 16:13:20 +0200
Local: Tues, Feb 22 2011 9:13 am
Subject: Re: find in a nested object

Do you only need the child?
Just do Child.find_by_name_and_parent_id
On Feb 22, 2011, at 16:12 , Michal Yad-Shalom wrote:

--
Dimitri Krassovski

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michal Yad-Shalom  
View profile  
 More options Feb 22 2011, 9:18 am
From: Michal Yad-Shalom <michal...@gmail.com>
Date: Tue, 22 Feb 2011 16:18:48 +0200
Local: Tues, Feb 22 2011 9:18 am
Subject: Re: find in a nested object

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?

On Tue, Feb 22, 2011 at 4:13 PM, Dimitri Krassovski <lab...@startika.com>wrote:

--
Michal Yad Shalom
054-4315617

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dimitri Krassovski  
View profile  
 More options Feb 22 2011, 9:23 am
From: Dimitri Krassovski <lab...@startika.com>
Date: Tue, 22 Feb 2011 16:23:07 +0200
Local: Tues, Feb 22 2011 9:23 am
Subject: Re: find in a nested object

@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.

On Feb 22, 2011, at 16:18 , Michal Yad-Shalom wrote:

--
Dimitri Krassovski

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Elad Meidar  
View profile  
 More options Feb 22 2011, 10:00 am
From: Elad Meidar <eize....@gmail.com>
Date: Tue, 22 Feb 2011 17:00:40 +0200
Local: Tues, Feb 22 2011 10:00 am
Subject: Re: find in a nested object

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

On Tue, Feb 22, 2011 at 4:23 PM, Dimitri Krassovski <lab...@startika.com>wrote:

--
Elad Meidar,
CEO, Nautilus6 Inc.
http://blog.eizesus.com
http://www.nautilus6.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michal Yad-Shalom  
View profile  
 More options Feb 22 2011, 9:55 am
From: Michal Yad-Shalom <michal...@gmail.com>
Date: Tue, 22 Feb 2011 16:55:54 +0200
Local: Tues, Feb 22 2011 9:55 am
Subject: Re: find in a nested object

Dimitri Thanks.

BTW... your "rudeness" was in place.

On Tue, Feb 22, 2011 at 4:23 PM, Dimitri Krassovski <lab...@startika.com>wrote:

--
Michal Yad Shalom
054-4315617

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »