简体   繁体   English

通过两个联接表进行Rails HABTM关联

[英]Rails HABTM association through two join tables

I have the following models: 我有以下型号:

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => :friend_id
  has_and_belongs_to_many :friend_groups
end

class FriendGroup < ActiveRecord::Base
  has_and_belongs_to_many :friendships
end

How can I declare that FriendGroup has_and_belongs_to_many :friends through :friendships ? 如何声明FriendGroup has_and_belongs_to_many :friends through :friendships

I would like to have FriendGroup.friend_ids , so in the form I can just set a field 'friend_group[friend_ids][]' and just call FriendGroup.create(params[:friend_group]) without any additional logic. 我想拥有FriendGroup.friend_ids ,因此在表格中我可以设置一个字段'friend_group[friend_ids][]'并直接调用FriendGroup.create(params[:friend_group])而无需任何其他逻辑。

类FriendGroup <ActiveRecord :: Base has_and_belongs_to_many:friends,:through =>:friendgroups end

Well, I found not the one-line, but two-line solution which looks elegant enough: 好吧,我发现不是单行的,而是两行的解决方案,看起来很优雅:

class FriendGroup < ActiveRecord::Base
...
  attr_accessor :friend_ids
  before_create {|fg| fg.friendships = Friendship.find_all_by_user_id_and_friend_id(fg.user_id, fg.friend_ids)}

I'm confused on what you are trying to do with FriendGroups. 我对您要如何使用FriendGroups感到困惑。

Your basic Friendship is modeled as: 您的基本友谊模型如下:

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User"
end

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, :through => :friendships
end

Are you looking to mass create Friendship records between all the Users that you pass in? 您是否要在传入的所有用户之间大规模创建友谊记录? That's probably some sort of permutations problem. 这可能是某种排列问题。 Not something you would need another model for. 不需要其他模型。 Maybe a class method on Friendship like interconnect(user_ids) . 也许在Friendship上有一个类方法,例如interconnect(user_ids)

If you're wanting to find groups of Users that are all friends of each other that sounds like you're getting into Graph Theory and connectivity. 如果您想找到彼此都是朋友的用户组,那听起来就像您正在进入图论和连通性。

EDIT: 编辑:

In the case of FriendGroups just being generic containers of friends with a name attached I would do something like this: 在FriendGroups只是带有名称附加的朋友的通用容器的情况下,我将执行以下操作:

class User < ActiveRecord::Base
  has_many :friend_groupings
  has_many :friend_groups
  has_many :friendships
  has_many :friends, :through => :friendships
end

class FriendGrouping < ActiveRecord::Base
  belongs_to :friend_group
  belongs_to :friend
end

class FriendGroup < ActiveRecord::Base
  has_many :friend_groupings
  has_many :friends, :class_name => "User", :through => :friend_groupings
  belongs_to :user

  validates_presence_of :name # Name of FriendGroup
end

I would probably nest FriendGroups under Users, and have FriendGroups accept_nested_attributes_for FriendGroupings. 我可能会将FriendGroups嵌套在Users下,并让FriendGroups accept_nested_attributes_for FriendGroupings。 I would do this in the FriendGroup controller and in the view for the FriendGroup allow the user to set the name of the group and then give them checkboxes for each of their friends to put in the group. 我将在FriendGroup控制器中执行此操作,并在FriendGroup的视图中允许用户设置组的名称,然后为他们的每个朋友提供复选框以放入组中。 For each friend they select create a new FriendGrouping between the FriendGroup and the User. 他们为每个朋友选择在FriendGroup和User之间创建一个新的FriendGrouping。 That should get you what you want. 那应该给您您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM