Ruby on rails 如何在Rails中通过两级关联确定模型的范围?
我有Ruby on rails 如何在Rails中通过两级关联确定模型的范围?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有用户,标签,标签 User.rb has_one :tagging belongs_to :user belongs_to :tag has_many :taggings 标记.rb has_one :tagging belongs_to :user belongs_to :tag has_many :taggings 标记.rb has_one :tagging belongs_to :user belongs_to :tag has_many :t
用户
,标签
,标签
User.rb
has_one :tagging
belongs_to :user
belongs_to :tag
has_many :taggings
标记.rb
has_one :tagging
belongs_to :user
belongs_to :tag
has_many :taggings
标记.rb
has_one :tagging
belongs_to :user
belongs_to :tag
has_many :taggings
我想根据标签的名称确定用户的范围。怎么做
目前我知道如何做一级关联下的范围,例如:
scope:with_tag\u id,->(tag\u id){joins(:tagging)。其中(taggings:{tag\u id:tag\u id})}
但是如何做更多的关卡呢?试试这个
class User < ActiveRecord::Base
scope :by_tag_name, ->(tag_name) { joins(tagging: :tag).where("tags.name=?", tag_id, tag_name)
end
class用户(标签名称){连接(标签::标签)。其中(“tags.name=?”,标签id,标签名称)
结束