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.
> 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 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> 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 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:
> 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 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:
>> 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 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> -- > Michal Yad Shalom > 054-4315617
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
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:
>> On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:
>> 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 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to >> israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/israelrb?hl=en.
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to >> israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/israelrb?hl=en.
> -- > Michal Yad Shalom > 054-4315617
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> -- > Dimitri Krassovski
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> 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: > 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:
>> I would like to find a child with name "moshe" not a parent.
>> On Tue, Feb 22, 2011 at 4:02 PM, Elad Meidar <eize....@gmail.com> wrote: >> parent = Parent.find_by_name("Elad", :include => :children)
>> On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:
>>> 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 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 israelrb@googlegroups.com. >>> To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. >>> For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
>> -- >> Michal Yad Shalom >> 054-4315617
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> -- > Dimitri Krassovski
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> -- > Michal Yad Shalom > 054-4315617
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.
> @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:
> 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:
>> 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:
>> I would like to find a child with name "moshe" not a parent.
>> On Tue, Feb 22, 2011 at 4:02 PM, Elad Meidar <eize....@gmail.com> wrote:
>>> On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:
>>> 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 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 israelrb@googlegroups.com. >>> To unsubscribe from this group, send email to >>> israelrb+unsubscribe@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/israelrb?hl=en.
>>> -- >>> 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 israelrb@googlegroups.com. >>> To unsubscribe from this group, send email to >>> israelrb+unsubscribe@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/israelrb?hl=en.
>> -- >> Michal Yad Shalom >> 054-4315617
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to >> israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/israelrb?hl=en.
>> -- >> Dimitri Krassovski
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to >> israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/israelrb?hl=en.
> -- > Michal Yad Shalom > 054-4315617
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> -- > Dimitri Krassovski
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> @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:
> 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:
>> 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:
>> I would like to find a child with name "moshe" not a parent.
>> On Tue, Feb 22, 2011 at 4:02 PM, Elad Meidar <eize....@gmail.com> wrote:
>>> On 22 בפבר 2011, at 15:50, michalyad <michal...@gmail.com> wrote:
>>> 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 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 israelrb@googlegroups.com. >>> To unsubscribe from this group, send email to >>> israelrb+unsubscribe@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/israelrb?hl=en.
>>> -- >>> 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 israelrb@googlegroups.com. >>> To unsubscribe from this group, send email to >>> israelrb+unsubscribe@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/israelrb?hl=en.
>> -- >> Michal Yad Shalom >> 054-4315617
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to >> israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/israelrb?hl=en.
>> -- >> Dimitri Krassovski
>> -- >> 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 israelrb@googlegroups.com. >> To unsubscribe from this group, send email to >> israelrb+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/israelrb?hl=en.
> -- > Michal Yad Shalom > 054-4315617
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.
> -- > Dimitri Krassovski
> -- > 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 israelrb@googlegroups.com. > To unsubscribe from this group, send email to > israelrb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/israelrb?hl=en.