Ruby on Rails Friday, September 20, 2013

On Fri, Sep 20, 2013 at 6:39 AM, Josh Jordan <josh.jordan@gmail.com> wrote:
> Felix,
>
> You defined image_dir as a local variable in the scope of the Job class. It
> goes out of scope (and, since nothing else references it, gets cleaned up)
> after the class definition of Job is evaluated. Instead, define image_dir on
> instances of Job:
>
> class Job < ActiveRecord::Base
> include ImageModel
>
> def image_dir
> @image_dir ||= 'jobs'
> end
> end
>
> Better yet, since image_dir is the same for every instance of Job, make it a
> class method:
>
> class Job < ActiveRecord::Base
> include ImageModel
>
> def self.image_dir
> @image_dir ||= 'jobs'
> end
> end
>
> and access it through the model's class:
>
> File.open(Rails.root.join('public', 'images', self.class.image_dir,
> new_image.original_filename), 'wb') do |f|

To add a note, if image_dir is persistently stored in the db you can
use the default opt in your Migration.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAM5XQnxA-94EY0NJeSEAw3ph57%3D5FWC8pKxQ%2BM67H204Ti7krg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment