Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: remove element from array by id

Is there some sort of short hand for

  @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 is an id of a notification stored into cookies. this is in an iteration, that removes notifications that the user doesn't want to see.

thanks! =)

like image 583
NullVoxPopuli Avatar asked Nov 30 '25 06:11

NullVoxPopuli


1 Answers

If this is an array of activerecord objects, you could delete from the database like this.

Notification.delete_all(:id => cookie[0].to_i)

If this is just an array, then you can use the delete if

@notifications.delete_if{|x| x == cookie[0].to_i}

like image 93
Geoff Lanotte Avatar answered Dec 02 '25 21:12

Geoff Lanotte