I want to generate CSV file after user creates his account, so I created after_save
filter in User
Model, but I'm getting errors.
Here is my code:
after_save :to_csv
def to_csv(options = {})
require 'csv'
CSV.generate(options) do |csv|
csv << self.column_names//also tried User.column_names
csv << self.attributes.values_at(*column_names)
end
end
but when User is created I get error:
undefined local variable or method `column_names' for #<User:0x326f778>
app/models/user.rb:52:in `block in to_csv'
app/models/user.rb:50:in `to_csv'
app/controllers/users_controller.rb:27:in `create'
Why I'm getting this ? I'm using this railscast - http://railscasts.com/episodes/362-exporting-csv-and-excel.
column_names
is a method on the class. You're calling it on the instance. If you really want column_names
then use self.class.column_names
.