使用acts_as_list gem时重置位置属性? (导轨) [英] Resetting position attribute when using acts_as_list gem? (Rails)

查看:164
本文介绍了使用acts_as_list gem时重置位置属性? (导轨)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Rails 3应用程序,其中一个目标有很多目标。在我看来,我允许用户为任何给定的目标重新排序目标,并且我正在使用 acts_as_list gem来实现这一功能。



目标模型:

  class Goal< ActiveRecord :: Base 
attr_accessible:name,:description
$ b has_many:goals,:order => :position,:dependent => :销毁
结束

目标模型:

  class Objective< ActiveRecord :: Base 
attr_accessible:name,:description,:position

elongs_to:goal

acts_as_list:scope => :目标
结束

我还希望允许用户更改特定目标目标与...相关。我假定每当一个目标的目标被改变时(因为我定义了 scope =>:goal ),那么 acts_as_list gem会重置位置列。 code>),但它只是保留了当前位置。 对于我来说,这是不可取的,因为这个位置在新目标的背景下不再有意义。我宁愿将位置重置,目标也会移动到与其关联的新目标的底部。所以我写了这个方法发生在Objective模型的 before_save 中:

  before_update:reset_position 

def reset_position
如果self.goal_id_changed?
self.move_to_bottom #acts_as_list method
end
end

不幸的是,当我尝试保存时,这会导致堆栈级别过深错误。如何解决这个问题的任何想法?
解决方案

好的,我最终重写了 reset_position因为 acts_as_list 显然不处理关联变化。

  before_update:reset_position 

def reset_position
如果self.goal_id_changed?
self.position = self.goal.objectives.count> 0? self.goal.objectives.last.position + 1:1
end
end



<基本上,它将目标移动到目标列表的底部,当它与不同的目标相关联时。似乎有效。


I have a simple Rails 3 app where a Goal has many Objectives. In my views, I allow the user to-reorder the Objectives for any given Goal, and I am using the acts_as_list gem to achieve some of this functionality.

Goal model:

class Goal < ActiveRecord::Base
  attr_accessible :name, :description

  has_many :objectives, :order => :position, :dependent => :destroy
end

Objective model:

class Objective < ActiveRecord::Base
  attr_accessible :name, :description, :position

  belongs_to :goal

  acts_as_list :scope => :goal
end

I also want to allow the user to change the Goal that a particular Objective is associated with. I would have assumed that the acts_as_list gem would reset the position column whenever an Objective's Goal is changed (because I defined scope => :goal), but instead it just retains the current position.

For me, this is undesirable, because the position no longer makes sense in the context of a new Goal. I would rather that the position gets reset, and the Objective is moved to the bottom of the new Goal that it is associated with. So I wrote this method to occur on before_save in the Objective model:

  before_update :reset_position

  def reset_position
    if self.goal_id_changed?
      self.move_to_bottom #acts_as_list method
    end
  end

Unfortunately, this results in a stack level too deep error when I attempt to save. Any ideas for how I can fix this?

解决方案

Okay, I ended up re-writing the reset_position to what is shown below, since acts_as_list clearly does not handle association changes.

before_update :reset_position

def reset_position
  if self.goal_id_changed?
    self.position = self.goal.objectives.count > 0 ? self.goal.objectives.last.position + 1 : 1
  end
end

Basically, it moves the Objective to the bottom of the Objective list when it is associated with a different Goal. Seems to work.

这篇关于使用acts_as_list gem时重置位置属性? (导轨)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆