:条件语句和Ruby Rails

时间:2013-02-25 03:32:14

标签: ruby-on-rails ruby

我正试图让这些陈述发挥作用:

@all_ratings = ["G","PG","PG-13","R"]
@valid_ratings = params["ratings"]
@movies = Movie.find(:all ,  :conditions => {@valid_ratings[:rating.upcase] => "1"}   )

但我收到了错误:

undefined method `to_sym' for nil:NilClass

当我得到一场比赛时。

示例输入是:

"ratings"=>{"PG-13"=>"1"}

我哪里错了?


更多信息:

该表有三个字段,标题,发布日期和评级,非常简单。评级选项在@all_ratings

中列出

2 个答案:

答案 0 :(得分:0)

它会在@valid_ratings

中告诉您nil

您可能正在尝试这样做?

@valid_ratings = Rating.find(params["ratings"])

答案 1 :(得分:0)

Rails 3.x:

@all_ratings = ["G","PG","PG-13","R"]
@valid_ratings = params["ratings"]
# Is @valid_ratings the same as you example, "ratings"=>{"PG-13"=>"1"}?
# It would be easiest to pass a subset of @all_ratings such that the params
# get converted to something like this: "ratings"=>["G", "PG"]
Movie.where(:rating => valid_ratings).all
# SQL: SELECT * FROM movies WHERE rating IN ('G','PG')

我不确定你在:rating.upcase尝试了什么。是否有一个名为评级的变量? :rating是一个符号。 upcase不是Symbol的方法。