是否有 Ruby 方法 returns 数组中最后删除的元素?
Is there a Ruby method that returns the last deleted element in an array?
这听起来可能有点奇怪,因为如果您从 some_array 中删除一个元素,它可能会从内存中消失。
但在某些情况下,获取最后删除的项目会很好,例如当跟踪最后一个取消关注的用户(即从他们的关注数组中删除)并在视图中呈现类似 'Bob has stopped following Smith' 的消息时。我不确定用什么方法可以做到这一点,因此才有了这个问题。
编辑:我认为在删除之前将项目存储在实例变量中无济于事,因为实例变量仅存在于单个控制器操作中。
以下是一些使用 public_activity gem:
的代码片段
class User < ApplicationRecord
# allow user to follow other users
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
def unfollow(other_user)
following.delete(other_user)
end
end
class Relationship < ApplicationRecord
include PublicActivity::Model
tracked owner: ->(controller, model) {controller && controller.current_user}
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
class RelationshipsController < ApplicationController
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
flash[:notice] = "You've stopped following #{user.name}"
redirect_to users_path
end
end
我希望我的 activities/index 路径呈现一个 _destroy 部分,它会说用户 x 停止关注用户 y',如下所示:
class ActivitiesController < ApplicationController
def index
@activities = PublicActivity::Activity.order("created_at desc")
end
end
并且在视图中:
<% @activities.each do |activity| %>
<%= link_to activity.owner.name, activity.owner if activity.owner %>
<%= render_activity activity %><br>
<% end %>
我无法在删除最后一个元素的方法中考虑 return 它...
如果你这样做:
var = ary.pop
将删除最后一个元素和 return 它,您可以将 var
保存在另一个 table 或其他任何...
如果你想要一个更强大的解决方案,你可以搜索 papertrail gem.
听起来你想要审计 gem。
试试看
https://github.com/collectiveidea/audited
或
这听起来可能有点奇怪,因为如果您从 some_array 中删除一个元素,它可能会从内存中消失。 但在某些情况下,获取最后删除的项目会很好,例如当跟踪最后一个取消关注的用户(即从他们的关注数组中删除)并在视图中呈现类似 'Bob has stopped following Smith' 的消息时。我不确定用什么方法可以做到这一点,因此才有了这个问题。
编辑:我认为在删除之前将项目存储在实例变量中无济于事,因为实例变量仅存在于单个控制器操作中。
以下是一些使用 public_activity gem:
的代码片段 class User < ApplicationRecord
# allow user to follow other users
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
def unfollow(other_user)
following.delete(other_user)
end
end
class Relationship < ApplicationRecord
include PublicActivity::Model
tracked owner: ->(controller, model) {controller && controller.current_user}
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
class RelationshipsController < ApplicationController
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
flash[:notice] = "You've stopped following #{user.name}"
redirect_to users_path
end
end
我希望我的 activities/index 路径呈现一个 _destroy 部分,它会说用户 x 停止关注用户 y',如下所示:
class ActivitiesController < ApplicationController
def index
@activities = PublicActivity::Activity.order("created_at desc")
end
end
并且在视图中:
<% @activities.each do |activity| %>
<%= link_to activity.owner.name, activity.owner if activity.owner %>
<%= render_activity activity %><br>
<% end %>
我无法在删除最后一个元素的方法中考虑 return 它...
如果你这样做:
var = ary.pop
将删除最后一个元素和 return 它,您可以将 var
保存在另一个 table 或其他任何...
如果你想要一个更强大的解决方案,你可以搜索 papertrail gem.
听起来你想要审计 gem。
试试看
https://github.com/collectiveidea/audited
或