中間テーブルを用いた以下のような
User<-Users_group->Group
アソシエーションを用いて、
group = Group.find(1) group.users => idが1のグループに属しているユーザー
と、取り出せるのはわかるのですが、逆にidが1のグループに属していないユーザーを取得するにはどうすればいいのか教えていただきたいです。
わかる方がいましたら、ご教授お願いします。
###テーブルとアソシエーション
users table
column | type |
---|---|
name | string |
string | |
password | string |
class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable #association has_many :users_groups has_many :groups, through: :users_groups end
groups table
column | type |
---|---|
name | string |
class Group < ApplicationRecord #association has_many :users_groups has_many :users, through: :users_groups accepts_nested_attributes_for :users_groups end
users_groups table
|column|type|
|:--|:--|:--|
|user_id|references|
|group_id|references|
class UsersGroup < ApplicationRecord #association belongs_to :user belongs_to :group end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/11 09:12