8
Dinlenme API'm ile model kaydetme konusunda bir sorunum var. Ben ilişkili Bir Kart birçok görevleri ile model ve bir Müşteri vardır:Modelde ActiveRecord :: AssociationTypeMismatch var Save
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
ben böyle bir json gönderdiğinizde:
{
"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"
}
]
}
}
eylem oluşturmak listem:
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
bunu yaparken, Ben bir tane aldım:
W şapka yanlış yaptım mı?
Teşekkür ederim ama şimdi ActiveRecord :: RecordNotFound var (model ID = ile ID = 6 ile Görev bulunamadı) model.save() – Sebastien
Üzgünüz, yeni görevler değil var olan kayıtların var olduğunu görmedim . Guess accepts_nested_attributes_for bu durumda yararsızdır, ilişkilendirmeyi değiştirmeye izin vermez. Nested_attributes.rb'yi yamalamaya, bir maymun yama oluşturmaya veya özel işleyici oluşturmaya çalışabilirsiniz. İşte neden uygulanmadığına dair daha fazla ayrıntı: https://github.com/rails/rails/issues/2925 – dimuch
Bu yüzden sadece Card.new ile ilişkili görevlerle yeni bir Kart oluşturamıyorum (params [: card]) json'umla mı? – Sebastien