2014-09-29 60 views
0

這一個沒有使用谷歌搜索,所以我別無選擇,只能用這樣的braincracker打擾你。Rails,formtastic和postgres:NotNullViolation

我有2個模型,通過HMT鏈接。 我也有ActiveAdmin。 當我嘗試從第一個模型創建對象時,postgres發射NotNullViolation,因爲,rails會嘗試進行連接,而不會實際在連接表中插入已創建對象的ID。

像這樣:

SQL (0.9ms) INSERT INTO "project_stone_joins" ("stone_id") VALUES ($1) [["stone_id",2]] 
PG::NotNullViolation: ERROR: null value in column "project_id" violates not-null constraint 
DETAIL: Failing row contains (null, 2).: INSERT INTO "project_stone_joins" ("stone_id") VALUES ($1) 
    (0.4ms) ROLLBACK 

尖尖的事情是:改變做就好了。沒有笑話,siree,它做得很好。

(0.3ms) BEGIN 
SQL (0.7ms) DELETE FROM "project_stone_joins" WHERE "project_stone_joins"."project_id" = $1 AND "project_stone_joins"."stone_id" = 4 [["project_id", 10]] 
SQL (0.3ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 2]] 
SQL (0.3ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 3]] 
SQL (0.3ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 16]] 
SQL (0.3ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 17]] 
SQL (0.3ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 28]] 
SQL (0.4ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 30]] 
SQL (0.5ms) INSERT INTO "project_stone_joins" ("project_id", "stone_id") VALUES ($1, $2) [["project_id", 10], ["stone_id", 42]] 
    (33.5ms) COMMIT 

所以,實際上導軌忘了把我的project_idproject_stone_joins。 或者我過度勞累,沒有看到重要的事情。 這裏的模型,模式,等等。

SCHEMA.RB------------------------ 
    create_table "stones", force: true do |t| 
    t.integer "stone_material_id" 
    t.string "name" 
    t.string "cover_file_name" 
    t.string "cover_content_type" 
    t.integer "cover_file_size" 
    t.datetime "cover_updated_at" 
    t.boolean "render" 
    end 

    create_table "projects", force: true do |t| 
    t.string "name" 
    t.integer "project_group_id" 
    t.text  "description" 
    t.string "cover_file_name" 
    t.string "cover_content_type" 
    t.integer "cover_file_size" 
    t.datetime "cover_updated_at" 
    t.boolean "ours" 
    end 

    create_table "project_stone_joins", id: false, force: true do |t| 
    t.integer "project_id", null: false 
    t.integer "stone_id", null: false 
    end 

PROJECT.RB-MODELS-------------------- 
class Project < ActiveRecord::Base 
    belongs_to :project_group 
    has_many :project_photos 
    has_many :project_stone_joins, dependent: :destroy 
    has_many :stones, through: :project_stone_joins 

    has_attached_file :cover, 
        styles: {thumb: '100x100#', medium:'300x300#'}, 
        default_url: 'project_covers/:style/missing.png' 
    validates_attachment_content_type :cover, 
            content_type: /\Aimage\/.*\Z/ 

    accepts_nested_attributes_for :project_stone_joins 

end 
PROJECT.RB-ADMIN------------------ 
ActiveAdmin.register Project do 
    permit_params :name, :project_group_id, :description, 
       :cover, :ours, stone_ids:[] 

    form do |f| 
    f.inputs do 
     f.input :project_group 
     f.input :stones 
     f.input :name 
     f.input :description 
     f.input :cover, as: :file 
     f.input :ours 
    end 
    f.actions 
    end 
end 
STONE.RB-MODELS--------------------------- 
class Stone < ActiveRecord::Base 
    belongs_to :stone_material 
    has_many :stone_photos 
    has_many :project_stone_joins, dependent: :destroy 
    has_many :projects, through: :project_stone_joins 
    has_many :varieties 

    has_attached_file :cover, 
        styles: {thumb: '100x100#', medium:'300x300#'}, 
        default_url: 'stone_covers/:style/missing.png' 
    validates_attachment_content_type :cover, 
            content_type: /\Aimage\/.*\Z/ 

    accepts_nested_attributes_for :project_stone_joins 

end 
STONE.RB-ADMIN------------------------------ 
ActiveAdmin.register Stone do 
    permit_params :name, :stone_material_id, :cover, :render, project_ids: [] 

    form do |f| 
    f.inputs do 
     f.input :stone_material 
     f.input :name 
     f.input :cover, as: :file 
     f.input :render 
    end 
    f.actions 
    end 
end 
PROJECT_STONE_JOIN.RB-MODEL------------------ 
class ProjectStoneJoin < ActiveRecord::Base 
    belongs_to :project 
    belongs_to :stone 
end 

那麼,怎麼了?行動不受限制,但創建不起作用和更新。

回答

0

好吧,它似乎是已知的問題。 它修復刪除「accep_nested_attributes_for」的聯接表

另外,請確保不添加dependent:destroy has_many:聯接,否則它也會產生錯誤。 以下是更多信息。 https://github.com/rails/rails/issues/16085