Ruby on rails 如何获取当前对象的id?

Ruby on rails 如何获取当前对象的id?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我希望能够执行current_user.collaborators以获取当前用户拥有的所有协作者。也就是说,当前用户所拥有的所有任务的所有用户(也称为合作者)。这就是为什么我继续筛选(collaborations表)以仅获取当前用户是其所有者的协作记录,然后通过上一个筛选器自然地获得了许多协作者。 尝试在查询中使用self.id进行筛选时,在Rails控制台中调用User.first.collaborations时,我遇到以下错误: NoMethodError (undefined method

我希望能够执行current_user.collaborators以获取当前用户拥有的所有协作者。也就是说,当前用户所拥有的所有任务的所有用户(也称为合作者)。这就是为什么我继续筛选(collaborations表)以仅获取当前用户是其所有者的协作记录,然后通过上一个筛选器自然地获得了许多协作者。 尝试在查询中使用
self.id
进行筛选时,在Rails控制台中调用
User.first.collaborations
时,我遇到以下错误:

NoMethodError (undefined method `id' for #<Collaboration::ActiveRecord_Relation:0x00007fb41c802460>)
NoMethodError(未定义的#方法'id'))
关于我应该用什么/做什么来达到我的目标,有什么建议吗?谢谢你的帮助

用户类

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :tasks
  has_many :collaborations, -> {joins(:task).where("tasks.user_id = ?", self.id)}
  has_many :collaborators, through: :collaborations, class_name: "User", source: :user
end
class Collaboration < ApplicationRecord
  validates :task_id, presence: true
  validates :user_id, presence: true

  belongs_to :task
  belongs_to :user
end
class Task < ApplicationRecord
  validates :content, presence: true
  validates :label, presence: true

  belongs_to :user #owner of the task
  has_many :collaborations
  has_many :collaborators, through: :collaborations, class_name: "User", source: :user
end
class用户{joins(:task).where(“tasks.user\u id=?”,self.id)}
有很多:协作者,通过::协作,类名:“用户”,源::用户
结束
协作类

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :tasks
  has_many :collaborations, -> {joins(:task).where("tasks.user_id = ?", self.id)}
  has_many :collaborators, through: :collaborations, class_name: "User", source: :user
end
class Collaboration < ApplicationRecord
  validates :task_id, presence: true
  validates :user_id, presence: true

  belongs_to :task
  belongs_to :user
end
class Task < ApplicationRecord
  validates :content, presence: true
  validates :label, presence: true

  belongs_to :user #owner of the task
  has_many :collaborations
  has_many :collaborators, through: :collaborations, class_name: "User", source: :user
end
类协作
任务类

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :tasks
  has_many :collaborations, -> {joins(:task).where("tasks.user_id = ?", self.id)}
  has_many :collaborators, through: :collaborations, class_name: "User", source: :user
end
class Collaboration < ApplicationRecord
  validates :task_id, presence: true
  validates :user_id, presence: true

  belongs_to :task
  belongs_to :user
end
class Task < ApplicationRecord
  validates :content, presence: true
  validates :label, presence: true

  belongs_to :user #owner of the task
  has_many :collaborations
  has_many :collaborators, through: :collaborations, class_name: "User", source: :user
end
类任务
你写:

  class User < ApplicationRecord
    has_many :collaborations, -> {joins(:task).where("tasks.user_id = ?", self.id)}
  end

“选项”部分将被视为User.id,而您需要的是用户对象及其对象的id,您需要稍微更改代码

remove与协作和获取记录的写入方法有很多关系

def collaborations
  User.joins(:task).where("tasks.user_id = ?", self.id)
end 
并使用user对象调用该方法

user = User.last
user.collaborations

您将获得特定用户的协作

您不能在
有许多关联的
中使用self

为了在构建查询时访问用户对象,用户将作为参数传递给块。因此,基本上,您可以做的是:

has_many :collaborations, -> (user){joins(:task).where("tasks.user_id = ?", user.id)}
现在您可以调用
User.last.collaborations
这应该行得通


详细信息请参阅此部分。

未定义的局部变量或方法“id…”
完全错误是什么?我这样做是为了让当前用户只拥有他是任务所有者的协作,而不是所有的协作。我今晚将尝试,因为我现在正在工作。只需添加类名即可获得当前任务ent用户id?这就是你说的吗?为什么lambda不指向用户?当你调用
当前用户时。使用我的答案中定义的代码进行协作时,它将只返回当前用户的任务。好吧,酷!我会尝试一下并让你知道。我对我必须添加代码(lambda)的事实有点怀疑我知道Rails应该为我做这件事,但看起来我遗漏了一些东西。我会告诉你结果。为什么lambda没有指向用户实例?我只是尝试了一下,实际上我只得到了当前用户的任务,就像你说的。我想做的是以下几点。我希望能够使用当前用户r、 collaborators以获取当前用户拥有的所有协作者。也就是说,所有用户(也称为协作者)在所有任务中,当前用户是其所有者。这就是为什么我继续筛选collaborations表,以仅获取当前用户是其所有者的协作记录,然后通过获取协作者,因为前面的筛选器,自然有许多协作者。请想一想,我想做的是:
Collaboration.joins(:task).where(“tasks.user\u id=?”,self.id)
。谢谢你给了我一个提示。然后我想要所有的用户(也称为合作者)如果当前用户是通过
的任务的所有者,则有许多:合作者
。请查看我对我的问题的第一个答案的最后评论。但再次感谢。很高兴你得到了答案,总是很乐意帮助,尽管我的答案不太可能是你需要的。@NeimadTL