0

I need generate a variable for send a the view, for example i have this:

def after_find
    unless self.photo.nil?
        if File.exist?(Rails.root + 'public/uploads/tournaments/' +  self.photo)
            self.photo = '/uploads/tournaments/' + self.photo
        else
            self.photo = "http://placehold.it/250x400"
        end
    else    
        self.photo = "http://placehold.it/250x400"
    end
end

It runs well that variable is an attribute of db migration,the problem is when I declare something that is not an attribute of db migration

self.variable1 = "hello world"

enter image description here

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    Possible duplicate of [Non persistent ActiveRecord model attributes](http://stackoverflow.com/questions/11986792/non-persistent-activerecord-model-attributes) – zwippie Jan 18 '16 at 13:05

1 Answers1

3

If you want to have an attribute on a model but this attribute is not part of your database you can define it as accessor. In your model just add

#app/models/tournament.rb
class Tournament < ActiveRecord::Base
   attr_accessor :variable1
end

EDIT:

However in this particular case you should consider the performance penalty from checking the filesystem every time you load a Tournament object.

Using gem like Carrierwave or Paperclip is a much better solution!

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63