ruby-on-rails – rails – 如何在保存后刷新关联

我有一个类别与项列表.项目有一个位置,类别有一个关系has_many:items,:order => “位置”.当用户更新位置值时,我想看到它的位置.我的位置是一个浮点数,允许在舍入数之间移动.
pos=item.category.items.map(&:id)
current_position=pos.index(id.to_i)
item.save # want to refresh the relationship here
pos_new=item.categoty.items.map(&:id)
# grabbing this since just accessing item isn't updated if positioning has changed
item_new=Item.find(id)
pos_new=item_new.category.items.map(&:id)
new_position=pos_new.index(id)
if current_position!=new_position
  is_moved=true # sent back in JSON to propagate a dynamic change.
end

上面的作品,但它似乎很冗长.有没有办法告诉项目保存类别关系需要刷新,因为订单可以更改?

提前

解决方法

您可以使用将从数据库中提取模型的item.reload,并且在下次调用关联时,它将重新读取.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...