1
是否可以更改has_and_belongs_to_many关联的属性名称?像更改has_and_belongs_to_many属性的名称
class User
has_and_belongs_to_many :users, as: :friends
这样User.friends返回其他用户而不是User.users?
是否可以更改has_and_belongs_to_many关联的属性名称?像更改has_and_belongs_to_many属性的名称
class User
has_and_belongs_to_many :users, as: :friends
这样User.friends返回其他用户而不是User.users?
你可以做
has_and_belongs_to_many :friends, class_name: 'User'
您可能需要指定连接表,根据您的设置
has_and_belongs_to_many :friends, class_name: 'User', join_table: '...'
可以阅读更多有关这一切在这里has_and_belongs_to_many
请问你的加入表看起来像? 'user_id,friend_id'? – spickermann