Ruby on rails 主干使用ID更新嵌套集合。(Rails后端)

Ruby on rails 主干使用ID更新嵌套集合。(Rails后端),ruby-on-rails,ruby-on-rails-3,backbone.js,backbone-relational,Ruby On Rails,Ruby On Rails 3,Backbone.js,Backbone Relational,我有一个有很多任务的模范约会 创建一个包含任务的新约会模型可以很好地处理主干。但是,当我尝试使用不同任务的ID更新模型时,它不起作用 我得到以下错误: ActiveRecord::AssociationTypeMismatch: Task(#1192000) expected, got ActiveSupport::HashWithIndifferentAccess(#2458300) 参数: {"appointment"=> {"id"=>"36", "custo

我有一个有很多任务的模范约会

创建一个包含任务的新约会模型可以很好地处理主干。但是,当我尝试使用不同任务的ID更新模型时,它不起作用

我得到以下错误:

ActiveRecord::AssociationTypeMismatch: Task(#1192000) expected, 
got ActiveSupport::HashWithIndifferentAccess(#2458300)
参数:

{"appointment"=>
   {"id"=>"36",
    "customer_id"=>"19",
     "employee_id"=>"10",
     "done"=>"",
     "notes"=>"",
     "starts_at"=>"2012-09-05 13:00:00 +0200",
     "ends_at"=>"2012-09-05 14:30:00 +0200",
     "bookingtype"=>"",
     "tasks"=>
         "[{\"created_at\"=>\"2012-09-04T13:37:17+02:00\",
           \"duration\"=>\"60\", \"id\"=>\"12\",
           \"task_category_id\"=>\"5\", \"updated_at\"=>\"2012-09-04T13:46:13+02:00\"}]",
     "appointment"=>"",
     "task_ids"=>"[\"12\"]"},
     "action"=>"update",
     "controller"=>"appointments",
      "id"=>"36"}
我知道问题在于请求中有任务和任务ID,但我不知道如何在主干中解决这个问题

我的更新方法如下所示:

    save: function() {
            var self = this;        

            var tasks = [];

            $("input:checked").each(function() {                   
               tasks.push($(this).val());
            });

            this.model.save({starts_at: this.$('#appointment_starts_at_modal').val(), employee_id: this.$('#employeeSelect').val(), customer_id: this.$('#customerSelect').val(),
                        "starts_at(5i)": this.$('#appointment_starts_at_5i_modal').val() , 
                        "ends_at(5i)":  this.$('#appointment_ends_at_5i_modal').val(), task_ids: tasks}, {
            success: function(model, resp) {
                self.model = model;   

                self.close();
            },
            error: function() {
                //new App.Views.Error();
            }
        });
        return false;
    },
"tasks"=>
     "[{\"id"=>\"12\", \"created_at\"=>\"2012-09-04T13:37:17+02:00\",
       \"duration\"=>\"60\", \"id\"=>\"12\",
       \"task_category_id\"=>\"5\", \"updated_at\"=>\"2012-09-04T13:46:13+02:00\"}]",

从错误上看,这听起来像是ruby的问题,我对ruby一点也不熟悉。但是,关于主干,在约会模型中有“tasks”和“task_id”属性应该不是问题。主干网很乐意将这些数据作为JSON数据发送到您的服务器。但是请注意,在主干网中使用嵌套集合时,将ID作为任务模型之外的属性传递的方式有点奇怪。:-)

不过,我可以从主干的角度谈谈我所看到的

我假设您的
tasks\u id
属性表示所有任务的id数组<代码>任务是任务JSON对象的数组()。在主干网中,当处理嵌套集合等时,每个任务的
id
属性通常是任务对象的一部分。因此,如果我制作了一个应用程序,它以数组的形式发送一组任务数据,它将发送如下所示:

    save: function() {
            var self = this;        

            var tasks = [];

            $("input:checked").each(function() {                   
               tasks.push($(this).val());
            });

            this.model.save({starts_at: this.$('#appointment_starts_at_modal').val(), employee_id: this.$('#employeeSelect').val(), customer_id: this.$('#customerSelect').val(),
                        "starts_at(5i)": this.$('#appointment_starts_at_5i_modal').val() , 
                        "ends_at(5i)":  this.$('#appointment_ends_at_5i_modal').val(), task_ids: tasks}, {
            success: function(model, resp) {
                self.model = model;   

                self.close();
            },
            error: function() {
                //new App.Views.Error();
            }
        });
        return false;
    },
"tasks"=>
     "[{\"id"=>\"12\", \"created_at\"=>\"2012-09-04T13:37:17+02:00\",
       \"duration\"=>\"60\", \"id\"=>\"12\",
       \"task_category_id\"=>\"5\", \"updated_at\"=>\"2012-09-04T13:46:13+02:00\"}]",
当我使用嵌套集合时,我基本上确保某些模型的ID和所有属性都由对象封装

// My fake JSON
{'id':'1', 'appointment':{
    'id':'50',
    'tasks':[
        {'id':'100', 'taskName':'groceries' /* etc. */},
        {'id':'200', 'taskName':'bank errand'}
    ]
}}
当我的约会模型收到这个提取的数据时,我将在我的
parse()
或修改的
set()
方法中处理它。我将演示如何使用
parse()

类似上面的东西。我的TasksCollection将定义
model:Task
,因此使用属性哈希重置将使用适当的数据(包括ID)填充嵌套在约会模型中的我的集合

我认为这并不能解决你的问题,但因为你提到了做事的主要方式,我认为(在许多例子中)用这种方式来说明可能会给你一些想法