Ruby on rails JSON API:如何确保资源属于当前用户?

Ruby on rails JSON API:如何确保资源属于当前用户?,ruby-on-rails,json,json-api,Ruby On Rails,Json,Json Api,需要一些关于JSON API结构的帮助 假设我们有以下几点: class User < ApplicationRecord has_many :posts end class Post < ApplicationRecord has_many :comments belongs_to :user end class Comment < ApplicationRecord belongs_to :post has_many :talkbacks end class

需要一些关于JSON API结构的帮助

假设我们有以下几点:

class User < ApplicationRecord
 has_many :posts
end

class Post < ApplicationRecord
 has_many :comments
 belongs_to :user
end

class Comment < ApplicationRecord
 belongs_to :post
 has_many :talkbacks
end

class Talkbacks < ApplicationRecord
 belongs_to :comment
end
/posts
/posts/:id
/posts/:id/comments
/comments
/comments/:id
/comments/:id/talkbacks
/talkbacks
/talkbacks/:id
如果我们想显示帖子,那么很容易确定帖子属于当前用户,前提是我们有一个令牌:

# /posts/:id
current_user.posts.find_by_id!(params_id)
但是,如果我们想要显示特定的对讲,则更难确保对讲属于用户:

# /talkbacks/:id

确保用户能够访问该对讲的最佳方式是什么?

您可以尝试以下方式:

talkback = Talkback.find(params[:id])
if talkback
  if talkback.comment.post.user == current_user
    # do stuff
  else
    # talkback doesn't belong to signed in user
  end
else
  # no talkback exists with that id in the database
end
或者,您可以将该逻辑封装在模型中,并使用如下方法:

talkback = Talkback.find(params[:id])
if talkback
  if talkback.comment.post.user == current_user
    # do stuff
  else
    # talkback doesn't belong to signed in user
  end
else
  # no talkback exists with that id in the database
end
对讲机控制器.rb talkbalk.rb(模型类)
如果未指定方法的参数,此方法将当前登录用户作为默认用户。

为什么不将用户id添加到talkbacks表中,然后设置它们之间的关联。我认为这样做会让你的生活更轻松,然后试着打一个深窝电话。(我也会考虑将TabkBub类的名称改为单数的“对讲机”,这样您就可以使用Rails的命名约定。)
你可以通过以下方法来实现这一点:通过。
看看这个。

你应该通过关系来充实你与
的关系。然后,执行查询就很容易了。您不需要将用户id添加到任务字段(也不应该添加,因为帖子应该处理该关联)。
has_one
关系允许您通过另一个模型有效地拥有一个
归属于
关系,并且消除了拥有联接表的需要

class User < ApplicationRecord
  has_many :posts
  has_many :comments, through: :posts
  has_many :talkbacks, through: :comments
end

class Post < ApplicationRecord
  belongs_to :user

  has_many :comments
  has_many :tasks, through: :comments
end

class Comment < ApplicationRecord
  belongs_to :post

  has_one :user, through: :post

  has_many :talkbacks
end

class Talkbacks < ApplicationRecord
  belongs_to :comment

  has_one :user, through: :comment
end
作为你帖子的旁白

current_user.posts.find_by_id!(params_id)

posts.find_by_id!()
相当于
posts.find()
,因此您不需要按id执行
零件。默认情况下,如果Rails无法使用
find
方法找到记录,它将引发异常,这与使用
find\u By\u id
方法相同。

Yap,谢谢。所以这里没有神奇的解决方案,只有深层次的查询?不幸的是,没有,没有当前的设置谢谢。这只是问题的一个例子,一篇文章实际上可能有多个用户。
# /talkbacks/:id
current_user.talkbacks.find_by_id!(params_id)
class User < ApplicationRecord
  has_many :posts
  has_many :comments, through: :posts
  has_many :talkbacks, through: :comments
end

class Post < ApplicationRecord
  belongs_to :user

  has_many :comments
  has_many :tasks, through: :comments
end

class Comment < ApplicationRecord
  belongs_to :post

  has_one :user, through: :post

  has_many :talkbacks
end

class Talkbacks < ApplicationRecord
  belongs_to :comment

  has_one :user, through: :comment
end
current_user.talkbacks.find(params[:id])
current_user.posts.find_by_id!(params_id)