2013-08-07 29 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