Ruby on rails 如何获取模型实例变量
我有一个简单的Rails应用程序,用于尝试和学习Rails 它有一个我使用ActiveRecord创建的数据库表:Ruby on rails 如何获取模型实例变量,ruby-on-rails,ruby,model,Ruby On Rails,Ruby,Model,我有一个简单的Rails应用程序,用于尝试和学习Rails 它有一个我使用ActiveRecord创建的数据库表: class CreateMovies < ActiveRecord::Migration def up create_table :movies do |t| t.string :title t.string :rating t.text :description t.datetime :release_date
class CreateMovies < ActiveRecord::Migration
def up
create_table :movies do |t|
t.string :title
t.string :rating
t.text :description
t.datetime :release_date
t.timestamps
end
end
def down
drop_table :movies
end
end
class CreateMovies
下面是我对应的模型类:
class Movie < ActiveRecord::Base
def self.all_ratings
%w(G PG PG-13 NC-17 R)
end
def name_with_rating()
return "#{@title} (#{@rating})"
end
end
class电影
当我在一个电影实例上调用name_并对其进行评分时,对于任何电影,它返回的都是“()”。要从Movie的实例方法中获取Movie实例的字段,正确的语法或方法是什么
请注意,数据库已正确填充了电影行等。我已经完成了rake db:create、rake db:migrate等操作。活动记录属性未作为实例变量实现。试一试
class Movie < ActiveRecord::Base
def name_with_rating()
return "#{title} (#{rating})"
end
end
class电影
类电影
然后在控制台Movie.first.name\u中使用_评级
class Movie < ActiveRecord::Base
def name_with_rating
"#{title} (#{rating})"
end
end