Ruby on rails RubyonRails:按id从数组中删除元素
有什么速记吗Ruby on rails RubyonRails:按id从数组中删除元素,ruby-on-rails,Ruby On Rails,有什么速记吗 @notifications = Notification.find(:all, :conditions => ['expires_at > ?', Time.now]) notif = Notification.find(:all, cookie[0].to_i) @notifications.delete(notif[0]) if not notif.empty? cookie是存储在cookie中的通知的id。 这是在一个迭代中,它删除用户不想看到
@notifications = Notification.find(:all, :conditions => ['expires_at > ?', Time.now])
notif = Notification.find(:all, cookie[0].to_i)
@notifications.delete(notif[0]) if not notif.empty?
cookie是存储在cookie中的通知的id。
这是在一个迭代中,它删除用户不想看到的通知
谢谢!=) 如果这是activerecord对象的数组,您可以像这样从数据库中删除
Notification.delete\u all(:id=>cookie[0]。to\u i)
如果这只是一个数组,那么可以使用delete If
@notifications.delete_if{| x | x==cookie[0]。to_i}
现在您可以使用delete_at(index)
方法:
array = ['item0', 'item1', 2, 3]
array.delete_at 1
# => "item1"
array.delete_at 2
# => 3
array
# => ["item0", 3]
你可以用slice做同样的事情!(索引)
注意:delete\u at
是一种变异方法,即使它不是以结尾代码>,因此不要依赖其返回值,它将只是已删除的项目
参考:
数组没有方法,请全部删除=\这是activerecord对象的数组吗?如果是这样,那么您只想从数组中删除还是从数据库中删除?我添加了一个要从数组中删除的一行程序。不完全确定@notifications现在是什么。close、@notifications.delete_如果{x | x.id==cookie[0]。to|i}成功了。谢谢你的帮助!