ActiveRecord无法保存记录:"零长度分隔标识符"

时间:2013-08-07 14:33:29

标签: ruby-on-rails postgresql activerecord

我正在开发一个允许用户评价食谱的项目。我创建了一个简单的连接表(RecipeRating),User和Recipe都共享,但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

1 个答案:

答案 0 :(得分:1)

我通过给RecipeRating表提供了自己的主键来解决这个问题。