delvison
delvison

Reputation: 149

Using an attr within a model

Hey so I'm trying to use the :id attribute of a model within the model but i cannot figure it out.

 require 'zip/zip'
 require 'zip/zipfilesystem'

 class Lesson < ActiveRecord::Base
 attr_accessible :attachment, :course_id, :goal, :title, :assets_attributes
 belongs_to :course
 has_many :assets, :dependent => :destroy
 accepts_nested_attributes_for :assets, :reject_if => lambda { |a| a[:asset_file_name].blank? }


 def bundle
 bundle_filename = 'public/attachments/#{@lesson.id}.zip'

 Zip::ZipFile.open(bundle_filename, Zip::ZipFile::CREATE) {
   |zipfile|
   self.assets.collect {
     |asset|
       zipfile.add( "#{asset.id}", "public/attachments")
     }
 }

File.chmod(0644, bundle_filename)
self.save
end
end

im having issues in the bundle_filename = 'public/attachments/#{@lesson.id}.zip'

specifically @lesson.id part.

any ideas?

Upvotes: 0

Views: 35

Answers (2)

Carson Cole
Carson Cole

Reputation: 4461

@lesson does not appear to be set anywhere. Perhaps you meant @lesson as an instance of your class Lesson? If so, then @lesson would be self and your code would read

bundle_filename = "public/attachments/#{self.id}.zip"  

or just

bundle_filename = "public/attachments/#{id}.zip"

Upvotes: 0

carpamon
carpamon

Reputation: 6633

You should use double quotes to be able to evaluate ruby objects within a String using #{}.

bundle_filename = "public/attachments/#{@lesson.id}.zip"

Edit: You should define @lesson somewhere or just use self.id if that's what you are looking for.

Upvotes: 1

Related Questions