2012-06-04 34 views
8

Tôi gặp sự cố khi lưu mô hình với API còn lại của mình. Tôi có một mô hình thẻ với nhiều nhiệm vụ và một khách hàng có liên quan:Got ActiveRecord :: AssociationTypeMismatch trên mô hình lưu

class Card < ActiveRecord::Base 
    belongs_to :customer 

    has_many :card_tasks 
    has_many :tasks, :through => :card_tasks 

    accepts_nested_attributes_for :tasks 
    accepts_nested_attributes_for :card_tasks 
    accepts_nested_attributes_for :customer 

end 


class CardTask < ActiveRecord::Base 
    belongs_to :task 
    belongs_to :card 

    accepts_nested_attributes_for :task 
    accepts_nested_attributes_for :card 
end 

class Task < ActiveRecord::Base 
    has_many :cards, :through => :card_tasks 
    has_many :card_tasks 
end 

Khi tôi gửi một json như thế này:

{ 
    "card" = > { 
     "miscellaneous" = > "Obervations diverses", 
     "heater" = > "0", 
     "water_quality" = > "", 
     "customer" = > { 
      "id" = > "2", "house_name" = > "house_name2", "city" = > "city_2", "lastname" = > "lastname2", "sci" = > "sci2", "postal_code" = > "potal_code_2", "address_line_1" = > "address_line_2", "updated_at" = > "2012-03-05 18:20:57 +0000", "created_at" = > "2012-03-05 18:20:54 +0000", "firstname" = > "firstname2", "address_line_2" = > "address_line_3", "water_used" = > "0" 
     }, 
     "tasks" = > [ 
      { 
      "title" = > "Nettoyage ligne eau", "id" = > "6", "updated_at" = > "2012-02-17 08:40:47 +0000", "created_at" = > "2012-02-17 08:40:47 +0000" 
      }, 
      { 
      "title" = > "Surveillance", "id" = > "4", "updated_at" = > "2012-02-17 08:40:47 +0000", "created_at" = > "2012-02-17 08:40:47 +0000" 
      } 
     ] 
    } 
} 

tôi tạo ra hành động:

def create 
     card = Card.new(params[:card]) 
     if (card.save) 
     respond_with({ :card => card} , :location => nil, status: :created) and return 
     end 
     respond_with({ :errors => card.errors }, :location => nil, status: :unprocessable_entity) and return 
    end 

Khi làm điều này, i got a:

ActiveRecord::AssociationTypeMismatch (Task(#70249431354580) expected, got ActiveSupport::HashWithIndifferentAccess(#70249421573300)): 
    app/controllers/cards_controller.rb:14:in `new' 
    app/controllers/cards_controller.rb:14:in `create' 

W mũ tôi đã làm sai?

Trả lời

29

Vấn đề nằm trong cấu trúc JSON, Rails mong đợi tasks_attributes, chứ không phải tasks. Kiểm tra this question để biết chi tiết.

+0

Cảm ơn bạn nhưng bây giờ tôi đã nhận ActiveRecord :: RecordNotFound (Không thể tìm thấy Tác vụ có ID = 6 cho Thẻ có ID =) trên model.save() – Sebastien

+2

Xin lỗi, không thấy các tác vụ đó là các bản ghi hiện có, chứ không phải các bản ghi mới . Đoán accepts_nested_attributes_for là vô ích trong trường hợp đó, nó không cho phép thay đổi liên kết. Bạn có thể thử vá nested_attributes.rb, tạo một bản vá khỉ hoặc tạo trình xử lý tùy chỉnh. Dưới đây là thông tin chi tiết tại sao nó không được triển khai: https://github.com/rails/rails/issues/2925 – dimuch

+0

Vì vậy, tôi không thể tạo Thẻ mới với các tác vụ liên quan đến việc thực hiện Thẻ.new (params [: card]) với json của tôi? – Sebastien

Các vấn đề liên quan