Hey,
So I have a Page object with a to-many relationship to PagePart like
this:
has_many :parts, :class_name => ‘PagePart’, :dependent => :destroy
and due to this if I have a Page object called @page
@page.parts gives me an array of PagePart objects
I know this because if I do
logger.debug @page.parts.class
I see that the class is Array.
So now I look up the Ruby API and I find this nice find method:
Passes each entry in enum to block. Returns the first for which block
is not false. If no object matches, calls ifnone and returns its result
when it is specified, or returns nil
(1…10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
(1…100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35
So, I figure I can do:
@page.parts.find { |part| part.name = “some_name” }
But noooo
apparently the correct way to do this is:
@page.parts.find_by_name( “some_name” )
according to my error messages @page.parts.find expects only one
argument and it should be an id
so it’s basically find_by_id
So I would use:
@page.parts.find_by_name
But this does a fresh SQL select and gives me different PagePart
objects than exist in the @page.parts array
ahhh!!!
So, please somebody help me figure out how I can get that find method
to work on @page.parts
so that I get the objects already in the @page.parts array instead of
fresh copies of those objects from the DB.
thanks,
Jacob