2013-08-07 26 views
0

我正在研究一个允许用户对食谱评分的项目。我做了一个简单的连接表(RecipeRating),用户和配方都共享,但ActiveRecord无法保存。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

试试这个,以防万一:'@rating = @ user.recipe_ratings.find_by_recipe_id(@ recipe.id)' – Raindal

回答

1

我通过给RecipeRating表自己的主键固定此。

+0

为什么你没有一个ID开头? – drhenner