Ruby on rails 4 钢轨拔出误差

Ruby on rails 4 钢轨拔出误差,ruby-on-rails-4,Ruby On Rails 4,#用户:0x00000007234e28的未定义方法“pull”时出错 我想了解最后两个注册用户的详细信息 def index if User.exists? user1 = User.first.pluck(:id) user2 = User.second.pluck(:id) end end 要解决您的问题,您可以执行以下操作 def index last_two_users = User.order(created_at: :asc).

#用户:0x00000007234e28的未定义方法“pull”时出错

我想了解最后两个注册用户的详细信息

def index
    if User.exists?
        user1 = User.first.pluck(:id)
        user2 = User.second.pluck(:id)
    end
end

要解决您的问题,您可以执行以下操作

def index
  last_two_users = User.order(created_at: :asc).limit(2).pluck(:id)
end
如果有
id
列,您可以在
created_at
列上排序。如果使用
id
列,请将
asc
更改为
desc

另一种方法是使用地图:

last_two_users = User.last(2).map(&:id)

last\u two\u users
将最后两个用户添加到您的表中。

User.first在选择第一个元素时,您可以使用“User.first.id”来获取id,
pulk
仅适用于ActiveRecord数组。