This might be more of a general Ruby question than a Rails question. I have a model Upload, with subclasses such as Image, Video, Document, etc; that way I could check what type of file has been uploaded and create the relevant object, and based on what object it is, make a thumbnail or screencap or excerpt, whatever. What I wanted to do is something along the lines of "upload Upload.new(params[:upload])" within the controller, and then override Upload''s initialize method so it''s something like: def initialize(attributes) case attributes[:file].content_type when /^image/ Image.new(attributes) else super end end The problem is that the model Image < Upload I guess inherits Upload''s initialize method, so then it checks if the uploaded file is an image and then tries to create another Image object, which checks if it is an image and tries to create another Image object, and so on, until the "stack level too deep". I can''t seem to think of a way around this, unless I can somehow omit the case part of the initialize method within subclasses. Any 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Van Holstyn
2007-Jan-04 06:48 UTC
Re: Overriding initialize without causing infinite loop
I think you might want to change your approach a little. Even if there was no infinite loop, calling Image.new in your initialize method would not cause your variable ''upload'' to have and Image in it, it would still just have an Upload in it. You might want to try something like this... class Upload def self.create_from( attributes ) case attributes[:file].content_type when /^image/ Image.new( attributes ) #..... else Upload.new( attributes ) end end end class Image < Upload; end upload = Upload.create_from( params[:upload] ) Mark On 1/4/07, R Mason <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > This might be more of a general Ruby question than a Rails question. > > I have a model Upload, with subclasses such as Image, Video, Document, > etc; that way I could check what type of file has been uploaded and > create the relevant object, and based on what object it is, make a > thumbnail or screencap or excerpt, whatever. > > What I wanted to do is something along the lines of "upload > Upload.new(params[:upload])" within the controller, and then override > Upload''s initialize method so it''s something like: > > def initialize(attributes) > case attributes[:file].content_type > when /^image/ > Image.new(attributes) > else > super > end > end > > The problem is that the model Image < Upload I guess inherits Upload''s > initialize method, so then it checks if the uploaded file is an image > and then tries to create another Image object, which checks if it is an > image and tries to create another Image object, and so on, until the > "stack level too deep". > > I can''t seem to think of a way around this, unless I can somehow omit > the case part of the initialize method within subclasses. Any ideas? > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Mark Van Holstyn mvanholstyn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://lotswholetime.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
R Mason wrote:> > This might be more of a general Ruby question than a Rails question. > > I have a model Upload, with subclasses such as Image, Video, Document, > etc; that way I could check what type of file has been uploaded and > create the relevant object, and based on what object it is, make a > thumbnail or screencap or excerpt, whatever. > > What I wanted to do is something along the lines of "upload > Upload.new(params[:upload])" within the controller, and then override > Upload''s initialize method so it''s something like: >I would suggest creating a new class method for Upload, say new_for(). For example, def Upload.new_for(attributes) case attributes[:file].content_type when /^image/ return Image.new(attributes) when /^video/ return Video.new(attributes) when /^doc/ return Document.new(attributes) end nil end Then use upload = Upload.new_for(params[:upload]) instead. --Long http://MeandmyCity.com/ http://edgesoft.ca/blog/read/2 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>cause your variable ''upload'' to have and Image in it, it would still >just have an Upload in it. You might want to try something like this...Good point, I wasn''t even thinking of that. Hmm, I was thinking something along those lines, although I was going to avoid it if possible because I felt Upload.new(params[:upload) was more obvious to people who read the source than what you both suggested that would instead create the necessary object. I have been somewhat stubborn about it, but perhaps my approach won''t work at all. I also figured if I could override intitialize() successfully, I could also use other ActiveRecord methods that instantiate new objects, like Upload.create(), and not have to worry about writing a new method that would make the new object + save it. ie I thought hitting it right at the heart would solve problems down the line. Am I wrong on that one? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Corcoran
2007-Jan-04 15:34 UTC
Re: Overriding initialize without causing infinite loop
R - you are correct in wanting to get it right from the start but using "initialize" in your approach won''t work. I am with Mark and Long on this. This is the object oriented way of doing it. This allows you to instantiate the proper class based on some outside information. On the other hand, when you *know* you want to create an Image object you just call Image.create directly. By the way, Upload should be an abstract superclass and never instantiated. You can specify this in rails by adding the following line to your Upload model: self.abstract_class = true However, don''t use abstract_class if you are using Single Table Inheritance (STI). -Paul --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry I''m gonna bump this ;_; I''m still curious about my idea :) -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Why would that be the object oriented way of doing it? Or rather, why so that way more so than the way I suggested? I still sort of feel my concept (even though implementation apparently fails hard) is more DRY, simplistic, and easier to understand for cold readers. Is there any way at all for me to make it so that Upload.new() would create the necessary object, or is my idea just absurd and unlikely? Sorry if I come off as an ass. I have implemented something along the lines of what was suggested to me for now, but there''s still this nagging feeling that there''s a better way to pull it off. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Schuerig
2007-Jan-06 20:37 UTC
Re: Overriding initialize without causing infinite loop
On Saturday 06 January 2007 21:12, R Mason wrote:> I still sort of feel my concept (even though implementation > apparently fails hard) is more DRY, simplistic, and easier to > understand for cold readers. Is there any way at all for me to make > it so that Upload.new() would create the necessary object, or is my > idea just absurd and unlikely?How about something like this? class Upload # a PORO, not an AR::Base subclass def initialize(attributes ... @asset = Image.new(...) ... end end class Asset < ActiveRecord::Base ... end class Image < Asset ... end Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---