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" Ideas? -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 February 2011 06:54, Shea Barton <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thanks for the help. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 February 2011 09:12, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:>> 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.+1 The functionality you''re interested in should be called from the controller. # pseudo-Ruby upload = Upload.new(params[:upload]) if Upload.find_by_file_name(upload.file_name) upload = Upload.find_by_file_name(upload.file_name) end You can''t change "self" to "be" a different instance, but you can overwrite a variable containing one instance, with another instance. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
I know I have to do it in the controller is just wish there was some way I could put it into the model that way I never have to worry about it in my "skinny controller". -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Feb-19 18:35 UTC
Re: Re: ActiveRecord: Can''t change the value of self
I assume you''re replying to my post... but because you cut everything, I don''t know for sure... On 19 February 2011 17:36, Shea Barton <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I know I have to do it in the controller is just wish there was some way > I could put it into the model that way I never have to worry about it in > my "skinny controller".find_or_initialize_by_? Although, if you''re doing some fudging with MD5 hashes, you may not be able to use the dynamic methods.... # upload.rb def self.find_or_initialize_by_params(params) upload = Upload.new(params) return Upload.find_by_upload_file_name(upload.upload_file_name) || upload end #controller upload = Upload.find_or_initialize_by_params(params) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.