0

我正在尝试使用以下查询取回一条记录:

Rating.where(recommendation_id:1773452, rating_set:18, product_id:2086981)

返回:

 => [#<Rating id: 931, label: "Fair", rating: 2.0, created_at: "2013-02-21 22:20:19", updated_at: "2013-02-25 06:38:51", recommendation_id: 1773452, notes: "", rating_set: 18, product_id: 2086981, best_one: 1773452>]

我试图让一个属性显示在我的视图中。例如:

Rating.where(recommendation_id:rec.id, rating_set:params[:rating_set_id], product_id:params[:product_id]).pluck(:label)

它返回引号中的值["Fair"]

我怎样才能只取回价值Fair

4

2 回答 2

0
rating = Rating.where(recommendation_id:1773452, rating_set:18, product_id:2086981).first

rating.label # returns "Fair"
于 2013-02-25T07:40:55.037 回答
0

嗯。您不能直接获得“公平”的值。

因为 Active Record 总是会返回模型的对象。pluck 是一个额外的助手,它将带来集合。

当你用它查询时,.where("CONDITIONS")它总是会返回对象的集合。

所以在这种情况下,你必须这样做.first.label

我的建议是,如果您的条件相同,那么您可以使用find_by它将返回单个记录。

Rating.find_by_recommendation_id_and_rating_set_and_product_id(p1, p2, p3).label

于 2013-02-25T07:46:41.317 回答