我有一个包含项目列表的类别。这些项目有一个位置,并且类别与has_many有关系:items,:order =>“position”。当用户更新位置值时,我想看看它的位置。我的位置是允许在四舍五入数字之间移动的浮点数。rails - 保存后如何刷新关联
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
上面的工作,但它似乎很详细。有没有一种方法可以告诉物品,因为订单可能会更改,因此需要刷新类别关系?
还有谁会从搜索中找到这个答案(就像我刚才所做的那样):这种行为不再受Rails支持。您会收到弃用警告。集合的新方法是在集合代理上使用'reload'方法。 – klaustopher
@klaustopher谢谢。更新了我的答案。 –
“DEPRECATION WARNING:传递一个参数来强制关联重新加载现在已被弃用,并将在Rails 5.1中被删除,请调用父对象上的'reload'来代替(在(pry)处调用:2)” –
Dorian