如果我将movie = Movie.limit(1)
输入控制台,我会得到
SELECT "movies".* FROM "movies" LIMIT $1 [["LIMIT", 1]] => #<ActiveRecord::Relation [#<Movie id: 2, title: "Superman", rating: "PG", total_gross: 0.134218018e9, created_at: "2019-02-20 07:42:18", updated_at: "2019-02-20 08:17:24", description: "Clark Kent grows up to be the greatest super-hero", released_on: "1978-12-15">]>
但是,如果我输入movie.title
,而不是获得“超人”,我得到:
Traceback (most recent call last):
1: from (irb):2
Movie Load (0.6ms) SELECT "movies".* FROM "movies" LIMIT $1 [["LIMIT", 1]]
NoMethodError (undefined method `title' for #<Movie::ActiveRecord_Relation:0x000055b334842668>)
花了最后三个小时寻找原因无济于事。
编辑:我的班级有方法:
def index
@movies = Movie.all
end
但是当我从索引视图中调用它时,没有一个属性可以工作:
<% @movies.each do |f| %>
f.title
<% end %>
Movies#index中的ArgumentError
更新:我的数据库中有一个空行,每个属性返回nil。删除此行可修复此问题。