Forum: Ruby on Rails updating parent resource whenever child is updated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6c3acc025a63460a367b7981e606e032?d=identicon&s=25 Neubyr Neubyr (neubyr)
on 2009-04-29 02:01
I have nested resources as follows: 'albums has_many photos'.
Whenever someone edits photos, I would like update time stamp on albums.
I tried to do this like:
@photos.albums.updated_by = @currentusername
@photos.albums.save

But, this is not updating albums. I don't see anything happening in the
database log or mongrel log.

-
CS.
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-04-29 03:34
I think the new functionality in edge rails will do what you want:

http://afreshcup.com/2009/04/19/touch-your-active-...

You say:

class Photo
  belongs_to :user, :touch => true
end

This way, whenever you save or delete a photo instance, it'll 'touch'
its parent user, and update the updated_at timestamp.

This will require you to upgrade to edge rails, though.

Looking at your attempt, though, I'm not sure this is what you want. Are
you also using the "updated_by" attribute of the photo to save the id of
the user that changed it?

Chris




Carlos Santana wrote:
> I have nested resources as follows: 'albums has_many photos'.
> Whenever someone edits photos, I would like update time stamp on albums.
> I tried to do this like:
> @photos.albums.updated_by = @currentusername
> @photos.albums.save
>
> But, this is not updating albums. I don't see anything happening in the
> database log or mongrel log.
>
> -
> CS.
6c3acc025a63460a367b7981e606e032?d=identicon&s=25 Neubyr Neubyr (neubyr)
on 2009-04-29 03:44
Thanks Chris.
I am using created_at and updated_at attributes for timestamps. For
getting user info. though, I have creuser and upduser attributes.

Is there any other way to do this other than upgrading it to edge rails?

Thanks,
CS.

Chris Hanks wrote:
> I think the new functionality in edge rails will do what you want:
>
> http://afreshcup.com/2009/04/19/touch-your-active-...
>
> You say:
>
> class Photo
>   belongs_to :user, :touch => true
> end
>
> This way, whenever you save or delete a photo instance, it'll 'touch'
> its parent user, and update the updated_at timestamp.
>
> This will require you to upgrade to edge rails, though.
>
> Looking at your attempt, though, I'm not sure this is what you want. Are
> you also using the "updated_by" attribute of the photo to save the id of
> the user that changed it?
>
> Chris
>
>
>
>
> Carlos Santana wrote:
>> I have nested resources as follows: 'albums has_many photos'.
>> Whenever someone edits photos, I would like update time stamp on albums.
>> I tried to do this like:
>> @photos.albums.updated_by = @currentusername
>> @photos.albums.save
>>
>> But, this is not updating albums. I don't see anything happening in the
>> database log or mongrel log.
>>
>> -
>> CS.
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-04-29 04:01
Well, the updated_at timestamp for the album should update automatically
to the current time whenever the album model is changed and saved, so if
you're trying to do that then you don't need to use touching.

I'm confused at how your app is set up - if album has_many photos and
photo belongs_to album, then shouldn't your photo saving method use
singular objects? As in:

@photo.album.updated_by = @currentusername
@photo.album.save

That is, assuming you've defined "updated_by" in your migration as a
string to save the name of the current user - I think that's what you're
going for.

What I would do next is put debugger right between those two lines, then
call the save as a bang method (@photo.album.save!) and see what error
you get (if any).

Chris



Carlos Santana wrote:
> Thanks Chris.
> I am using created_at and updated_at attributes for timestamps. For
> getting user info. though, I have creuser and upduser attributes.
>
> Is there any other way to do this other than upgrading it to edge rails?
>
> Thanks,
> CS.
>
> Chris Hanks wrote:
>> I think the new functionality in edge rails will do what you want:
>>
>> http://afreshcup.com/2009/04/19/touch-your-active-...
>>
>> You say:
>>
>> class Photo
>>   belongs_to :user, :touch => true
>> end
>>
>> This way, whenever you save or delete a photo instance, it'll 'touch'
>> its parent user, and update the updated_at timestamp.
>>
>> This will require you to upgrade to edge rails, though.
>>
>> Looking at your attempt, though, I'm not sure this is what you want. Are
>> you also using the "updated_by" attribute of the photo to save the id of
>> the user that changed it?
>>
>> Chris
>>
>>
>>
>>
>> Carlos Santana wrote:
>>> I have nested resources as follows: 'albums has_many photos'.
>>> Whenever someone edits photos, I would like update time stamp on albums.
>>> I tried to do this like:
>>> @photos.albums.updated_by = @currentusername
>>> @photos.albums.save
>>>
>>> But, this is not updating albums. I don't see anything happening in the
>>> database log or mongrel log.
>>>
>>> -
>>> CS.
6c3acc025a63460a367b7981e606e032?d=identicon&s=25 Neubyr Neubyr (neubyr)
on 2009-04-29 04:23
It uses singular objects. I am sorry I typed it wrongly over here.

I tried to debug it using console. My observation was if:
@photo.album.upduser = @currentuser is same as previous user then
timestamps are not getting changes. However, @photo.album.save works and
returns true.

Any clues?

Thanks,
CS.

Chris Hanks wrote:
> Well, the updated_at timestamp for the album should update automatically
> to the current time whenever the album model is changed and saved, so if
> you're trying to do that then you don't need to use touching.
>
> I'm confused at how your app is set up - if album has_many photos and
> photo belongs_to album, then shouldn't your photo saving method use
> singular objects? As in:
>
> @photo.album.updated_by = @currentusername
> @photo.album.save
>
> That is, assuming you've defined "updated_by" in your migration as a
> string to save the name of the current user - I think that's what you're
> going for.
>
> What I would do next is put debugger right between those two lines, then
> call the save as a bang method (@photo.album.save!) and see what error
> you get (if any).
>
> Chris
>
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-29 04:33
(Received via mailing list)
Because the upduser is the same, it doesn't think that the object has
really changed, so doesn't update the timestamps.

We had a similar problem where we needed to update the timestamps on the
root object of a tree whenever anything in the tree changed. I ended up
implementing a touch method on the object, and called that in a
before_save hook.


  def touch
    orig_title = self.title
    self.title = ' '
    self.title = orig_title
    self.save
  end

  def after_save_hook
    root.touch
    true
  end

the edge rails code looks much nicer, but it will be a while before we
can move to newer rails.

Simon
6c3acc025a63460a367b7981e606e032?d=identicon&s=25 Neubyr Neubyr (neubyr)
on 2009-04-29 05:13
Thats weired..
I would expect parent.save to return false then.. :(

Thanks for the help.

CS.

Simon Macneall wrote:
> Because the upduser is the same, it doesn't think that the object has
> really changed, so doesn't update the timestamps.
>
> We had a similar problem where we needed to update the timestamps on the
> root object of a tree whenever anything in the tree changed. I ended up
> implementing a touch method on the object, and called that in a
> before_save hook.
>
>
>   def touch
>     orig_title = self.title
>     self.title = ' '
>     self.title = orig_title
>     self.save
>   end
>
>   def after_save_hook
>     root.touch
>     true
>   end
>
> the edge rails code looks much nicer, but it will be a while before we
> can move to newer rails.
>
> Simon
This topic is locked and can not be replied to.