> originally looking for the former behavior. The main idea
> was to do all my modification on objects in memory, then
> a final "a.save". The idea is that I'm putting off all
> my validation until one point in time when everything gets
> validated. I was trying to avoid having validations firing
> at varying times.
In your example, you're updating both the Author and Story tables. In this
case, the association is stored in the Story table (author_id). Thus, I
think that doing a final "a.save" with the intention of saving changes to
both the Author and Story is impossible, since the Author table doesn't
store any info about the story. I might be missing something, but I don't
see a way around doing 2 database calls in this case, so your validations
will trigger as each modification is saved.
> Perhaps it's not doable, or worth doing, but it did seem
> odd that this all works like a charm if you're dealing
> with *new* objects instead of objects reflecting records
> already in the database.
I think if you create the Story first, and the Author does not exist in
the db, then the Story will be held in memory and saved when you save the
Author. Maybe this is why you are seeing a difference?
Brian
> Thanks again.
>
> -glenn
>
> Peter Gumeson wrote:
>> You bet... any variation on the way you do your save.
>> The main improvement was the:
>> a.stories.find_by_title("The Lake")
>> instead of:
>> a.find_by_title("The Lake")
>> Peter
>> On 10/1/07, Glenn Little <[EMAIL PROTECTED]> wrote:
>>> Didn't see this when I sent the other. But yeah, I was
>>> sort of assuming the transaction wrapping. I was still hoping to do
all my in-memory attribute futzing as a separate step
>>> from the save() (seems clearer that way, allows for a single
>>> separate "a.valid?" call, etc).
>>> -glenn
>>> Peter Gumeson wrote:
>>>> Guess I should have read your whole post there...
>>>> I havn't tested this, but If you want it to be transactional, try
wrapping it in:
>>>> Author.transaction do
>>>> a = Author.find(some_id)
>>>> a.update_attributes!({middle_name => "Poindexter"})
>>>> a.stories.find_by_title("The Lake").update_attributes!({title =>
>>>> "The Dry Lake"})
>>>> end
>>>> (Be sure to use either update_attributes! or save! so it will throw
an
>>>> exception)
>>>> Peter
>>>> On 10/1/07, Peter Gumeson <[EMAIL PROTECTED]> wrote:
>>>>> How bout...
>>>>> a = Author.find(some_id)
>>>>> a.update_attribute(middle_name, "Poindexter")
>>>>> a.stories.find_by_title("The Lake").update_attribute(title, "The Dry
Lake")
>>>>> Peter
>>>>> On 10/1/07, Glenn Little <[EMAIL PROTECTED]> wrote:
>>>>>> Sorry, fixing confusing typo:
>>>>>> Glenn Little wrote:
>>>>>>> Say I have a couple of tables:
>>>>>>> author :has_many => stories
>>>>>>> story :belongs_to => author
>>>>>>> In a form handler I'd like to do something like:
>>>>>>> a = Author.find(some_id)
>>>>>>> a.middle_name = "Poindexter"
>>>>>>> s = a.find_by_title("The Lake")
>>>>>>> s.title = "The Dry Lake"
>>>>>>> I'd like to then transactionally (or even not) do
>>>>>>> a.save!
>>>>>>> and have the change to "s" get saved as well. Is this possible?
It works fine if s is a new record created with build(), or
something like
>>>>>>> s = Story.new
>>>>>>> a.stories << s
>>>>>>> a.save!
>>>>>>> Is there any magic like "<<" or build() that can be used for existing
>>>>>>> objects to attach them to a parent object for purposes of
>>>>>>> cascading the save()?
>>>>>>> Thanks!
>>>>>>> -glenn
>>>>>> _______________________________________________
>>>>>> Sdruby mailing list
>>>>>> [email protected]
>>>>>> http://lists.sdruby.com/mailman/listinfo/sdruby
>>>>> --
>>>>> "A flute with no holes is not a flute,
>>>>> and a doughnut with no hole is a Danish."
>>>>> - Zen philosopher, Basho
>>>> _______________________________________________
>>>> Sdruby mailing list
>>>> [email protected]
>>>> http://lists.sdruby.com/mailman/listinfo/sdruby
>>> _______________________________________________
>>> Sdruby mailing list
>>> [email protected]
>>> http://lists.sdruby.com/mailman/listinfo/sdruby
> _______________________________________________
> Sdruby mailing list
> [email protected]
> http://lists.sdruby.com/mailman/listinfo/sdruby
>
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby