Saturday, February 19, 2011

Re: [Rails] ActiveRecord: Can't change the value of self

On 19 February 2011 06:54, Shea Barton <lists@ruby-forum.com> wrote:
> I have a system where uploads are stored in a database with a filename
> based on the MD5 hash of the file contents. Following the logic of this,
> I assigned upload_file_name to be a unique index in my migration.
>
> What I want to do is, whenever a user tries to upload a file whose md5
> hash already exists in the database, just redirect to that other record
> rather than making a copy (which actually just throw an error because
> upload_file_name is a unique index)
>
> So I put in my model:
>
> self = other_upload if other_upload =
> Upload.find_by_upload_file_name(self[:upload_file_name])
>
> which ActiveRecord doesn't let me do saying "Can't change the value of
> self"

That is not an ActiveRecord issue it is a basic Ruby issue. Once you
are inside an instance method I do not think there is any way of
suddenly deciding you want to be in the method of a different object.
You could put something like the above code in a method of the model
that, rather than setting self, returns either itself or the
alternative, then call that method from the controller.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate