8
मुझे अपने बाकी एपीआई के साथ मॉडल सहेजने में समस्या मिली। मैं और कई कार्यों के साथ एक कार्ड मॉडल एक ग्राहक संबंधित हैं:मिला ActiveRecord :: एसोसिएशन टाइप टाइप करें मॉडल पर सहेजें
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
जब मैं भेज इस तरह एक json:
{
"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"
}
]
}
}
अपना कार्य बनाएँ:
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
यह कर रहे हैं, मुझे एक मिला:
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'
डब्ल्यू टोपी क्या मैंने गलत किया?
धन्यवाद, लेकिन अब मुझे ActiveRecord :: RecordNotFound (आईडी के साथ कार्ड के लिए आईडी = 6 के साथ कार्य नहीं मिल सका) =) – Sebastien
क्षमा करें, यह नहीं देखा कि कार्य मौजूदा रिकॉर्ड हैं, न कि नए । मान लें कि accepts_nested_attributes_for उस मामले में बेकार है, यह बदलते सहयोग की अनुमति नहीं देता है। आप nested_attributes.rb पैच करने का प्रयास कर सकते हैं, एक बंदर पैच बना सकते हैं, या कस्टम हैंडलर बना सकते हैं। यहां अधिक जानकारी दी गई है कि इसे कार्यान्वित क्यों नहीं किया गया है: https://github.com/rails/rails/issues/2925 – dimuch
इसलिए मैं कार्ड.न्यू (पैराम्स [: कार्ड] से जुड़े कार्यों के साथ एक नया कार्ड नहीं बना सकता। मेरे जेसन के साथ? – Sebastien