0

我正在开发一个允许用户对食谱进行评分的项目。我制作了一个 User 和 Recipe 共享的简单连接表 (RecipeRating),但 ActiveRecord 无法保存。

@rating = @user.recipe_ratings.where(:recipe_id => @recipe.id).first
@rating.rating = params[:rating]
@rating.save

产量:

ActiveRecord::StatementInvalid in UsersController#rate_recipe
PG::Error: ERROR:  zero-length delimited identifier at or near """"
LINE 1: ...013-08-07 14:28:39.965953' WHERE "recipe_ratings"."" IS NULL
                                                             ^
: UPDATE "recipe_ratings" SET "rating" = 3, "updated_at" = '2013-08-07 14:28:39.965953' WHERE "recipe_ratings"."" IS NULL

显然错误是由WHERE "recipe_ratings"."" IS NULL 什么引起的,我该如何解决?作为参考,创建或查找评级(按预期保存在我的数据库中)没有问题,它只是无法更新它们。

以供参考:

class User < ActiveRecord::Base
  ...
  has_many :recipe_ratings
end

class Recipe < ActiveRecord::Base
  ...
  has_many :recipe_ratings
end

class RecipeRating < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :user
  attr_accessible :comment, :rating, :user, :recipe#, :user_id, :recipe_id
end
4

1 回答 1

2

我通过为 RecipeRating 表提供自己的主键来解决此问题。

于 2013-08-11T05:30:59.890 回答