我有两个型号
class User < ActiveRecord::Base
has_and_belongs_to_many :shops
end
class Shop < ActiveRecord::Base
has_and_belongs_to_many :users
end
我必须找到与用户对象
关联的连接表users_shops中的行任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
为UserShop
class UserShop < ActiveRecord::Base
belongs_to :user
belongs_to :shop
end
然后对于某些user
你可以做
user_shops = UserShop.where(:user_id => user.id)
您还可以从UserShop
User
添加关系
class User < ActiveRecord::Base
has_and_belongs_to_many :shops
has_many :user_shops
end
给了一些user
,你可以做到
user_shops = user.user_shops