Felix Gläske
2013-Sep-18 13:11 UTC
Accessing model attributes in ActiveSupport::Concern module
I have some models which share the same functionality just on other paths.
So I decided to put these methods in a module and set the path in the
model. My problem is that I''m not able to access the attribute in my
module.
my model:
class Job < ActiveRecord::Base
include ImageModel
image_dir = "jobs"end
my module:
module ImageModel
extend ActiveSupport::Concern
def delete_image
unless pic_link == "" || pic_link == nil
begin
if File.delete(Rails.root.join("public", "images",
image_dir, pic_link))
return true
else
return false
end
rescue
return true #an error occured but when the image does not exist we
still return true
end
end
return true
end
def replace_image(new_image)
File.open(Rails.root.join("public", "images",
image_dir, new_image.original_filename), "wb") do |f|
if f.write new_image.read
delete_image
pic_link = new_image.original_filename
return true #everything went fine
else
return false #return false if new image could not be written
end
end
endend
The error I get:
undefined local variable or method `image_dir'' for
#<Job:0x007f8a93b9e8d8>
on this line:
File.open(Rails.root.join("public", "images", image_dir,
new_image.original_filename), "wb") do |f|
Did I miss something or did I oversee something important?
Felix
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/55a6bfd4-e4c5-4949-8db3-e3d9ac40bbdc%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Josh Jordan
2013-Sep-20 11:39 UTC
Re: Accessing model attributes in ActiveSupport::Concern module
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|
On Wednesday, September 18, 2013 9:11:59 AM UTC-4, Felix Gläske
wrote:>
> I have some models which share the same functionality just on other paths.
> So I decided to put these methods in a module and set the path in the
> model. My problem is that I''m not able to access the attribute in
my module.
>
> my model:
>
> class Job < ActiveRecord::Base
> include ImageModel
>
> image_dir = "jobs"end
>
> my module:
>
> module ImageModel
> extend ActiveSupport::Concern
>
> def delete_image
> unless pic_link == "" || pic_link == nil
> begin
> if File.delete(Rails.root.join("public",
"images", image_dir, pic_link))
> return true
> else
> return false
> end
> rescue
> return true #an error occured but when the image does not exist
we still return true
> end
> end
>
> return true
> end
>
> def replace_image(new_image)
> File.open(Rails.root.join("public", "images",
image_dir, new_image.original_filename), "wb") do |f|
> if f.write new_image.read
> delete_image
> pic_link = new_image.original_filename
> return true #everything went fine
> else
> return false #return false if new image could not be written
> end
> end
> endend
>
> The error I get:
>
> undefined local variable or method `image_dir'' for
#<Job:0x007f8a93b9e8d8>
>
> on this line:
>
> File.open(Rails.root.join("public", "images",
image_dir, new_image.original_filename), "wb") do |f|
>
> Did I miss something or did I oversee something important?
>
> Felix
>
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/0146f7a3-94ac-44e9-b77d-7d7945d7ae27%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2013-Sep-20 17:53 UTC
Re: Re: Accessing model attributes in ActiveSupport::Concern module
On Fri, Sep 20, 2013 at 6:39 AM, Josh Jordan <josh.jordan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.