加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:51:55 所属栏目:百科 来源:网络整理
导读:我有一个类别与项列表.项目有一个位置,类别有一个关系has_many:items,:order = “位置”.当用户更新位置值时,我想看到它的位置.我的位置是一个浮点数,允许在舍入数之间移动. pos=item.category.items.map(:id)current_position=pos.index(id.to_i)item.sav
我有一个类别与项列表.项目有一个位置,类别有一个关系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,并且在下次调用关联时,它将重新读取.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读