Ruby on rails 在RubyonRails中找到模型后创建一个变量
我需要为发送视图生成一个变量,例如:Ruby on rails 在RubyonRails中找到模型后创建一个变量,ruby-on-rails,ruby,Ruby On Rails,Ruby,我需要为发送视图生成一个变量,例如: 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://
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
变量是db迁移的一个属性,这运行得很好,问题是当我声明一些不是db迁移属性的东西时
self.variable1 = "hello world"
如果希望在模型上具有属性,但该属性不是数据库的一部分,则可以将其定义为访问器。在您的模型中,只需添加
#app/models/tournament.rb
class Tournament < ActiveRecord::Base
attr_accessor :variable1
end
#app/models/contraction.rb
类比赛
编辑:
但是,在这种特殊情况下,每次加载<代码>竞赛对象时,应该考虑检查文件系统的性能损失。
使用像Carrierwave或回形针这样的宝石是一个更好的解决方案 如果希望在模型上具有属性,但该属性不是数据库的一部分,则可以将其定义为访问器。在您的模型中,只需添加
#app/models/tournament.rb
class Tournament < ActiveRecord::Base
attr_accessor :variable1
end
#app/models/contraction.rb
类比赛
编辑:
但是,在这种特殊情况下,每次加载<代码>竞赛对象时,应该考虑检查文件系统的性能损失。
使用像Carrierwave或回形针这样的宝石是一个更好的解决方案 谢谢你的回答,我将添加这个宝石:D从那两个我推荐回形针移动谢谢你的回答,我将添加这个宝石:D从那两个我推荐回形针移动