无法在Rails中更新连接表中的额外属性

时间:2015-11-01 11:20:30

标签: ruby-on-rails

我在连接表中有一个属性:row_order(它也有自己的模型:TodolistsTodo),我正在尝试从另一个控制器(Todos)更新它。我怎样才能获得这个属性?

模型

double binary (double start, double end)
{
    for (;;)
    {
        double mid = (start + end) * .5;
        double delta=func(mid)-p;

        if ( delta < -eps)
            start = mid;
        else if (delta > eps)
            end = mid;
        else
            return mid;
    }
}

TodosController

class Todolist < ActiveRecord::Base
  has_many :todos, through: :todolists_todos
  has_many :todolists_todos
end

class Todo < ActiveRecord::Base
  include RankedModel
  ranks :row_order
  has_many :todolists, through: :todolists_todos
  has_many :todolists_todos
end

class TodolistsTodo < ActiveRecord::Base
  belongs_to :todolists
  belongs_to :todo
end

update_attribute行可以理解地失败,因为在哪里导致CollectionProxy和update_attribute方法只能处理单个结果。但是,我也不想更新_all。

Ajax调用控制器

def update_row_order
    @todo = @parent.todos.find(todo_params[:todo_id])
    @todo.todolists_todo.where(:todolist_id => @parent.id).update_attribute :row_order,     todo_params[:todolists_attributes][:row_order]
    @todo.save

    render nothing: true
  end

private

    def todo_params
      params.require(:todo).permit(:todo_id, :todotype, :description, :status, :help, todolists_attributes: [:id, :row_order, :todolist_id, :todo_id])
    end

1 个答案:

答案 0 :(得分:1)

知道你的TodolistsTodo:row_oder,并且知道,每个Todolist和ToDo只能有一个TodolistTodo,假设@parent是todolist,请使用

@parent = Todolist.find params[:todolist_id]
@item = @parent.todolists_todo.where(todo_id: params[:todo][:todo_id]).first
@item.update_attribute :row_order, todo_params[:todolists_attributes][:row_order]