本文介绍了模型设计:用户拥有的朋友是用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻求这种联系之前,我希望确保自己的方法正确。实施听起来太复杂了,所以我认为我的计划肯定有问题。我正在使用符合Rails存储约定的结构化(SQL)数据存储。我所拥有的是一个用户模型,它具有一个电子邮件地址, password_digest ,以及模式中的名称。

I'm looking to make sure my methods are correct before pursuing this association. The implementation sounds too complicated, so I think there must be something wrong with my plan. I am using structured (SQL) data storage, conforming to the rails storage conventions. What I have is a User model, it has an email address, password_digest, and name in the Schema.

class User < ActiveRecord::Base
  has_many :posts
end

我想实现与朋友集合的 has_many 关联,以便用户可以 belong_to 用户(作为朋友)。我希望 User.last.friends.last 在正确构建和填充后可以返回User对象。

I'd like to implement a has_many association to a friends collection, so that Users can belong_to Users (as friends). I'm hoping to be able to have User.last.friends.last return a User object when properly built and populated.

我相信我可以为该关联创建一个模型,例如:

I believe I can create a model for this association like:

Class Friend < ActiveRecord::Base
  belongs_to :user
  belongs_to :friendlies, class: 'User'
end
Class User < ActiveRecord::Base
  has_many :posts
  has_many :friends
  has_many :friendly, class: 'Friend'
end

但是我认为这需要我为模型添加一个,并使用 User.last.friends.last.user 所以我在想这是一个 has_and_belongs_to_many 关系。我可以避免使用以下内容(或类似内容)吗?

But I think that will require me to add an as to the models and query using User.last.friends.last.user So what I was thinking is this is a has_and_belongs_to_many relationship. Can I get away with the following (or something similar):

class User < ActiveRecord::Base
  has_and_belongs_to_many :friends, class: 'User'
end

我发现了:

class User < ActiveRecord::Base
  has_many :user_friendships
  has_many :friends, through: :user_friendships


class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'

和(自称为标准):

has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :dependent => :destroy
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id", :dependent => :destroy
has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :dependent => :destroy

我认为需要友谊模型。我觉得我不需要友谊模型。我认为类UserFriendship 方法看起来不错,但是它需要其他模型。现在进入问题:

Which I assume requires a Friendship model. I don't feel like I need a friendship model. I think that the class UserFriendship method looks right, but it requires an additional model. Now onto the questions:


  1. 我可以和以下人建立 has_and_belongs_to_many 关系吗?一个将用户与用户的朋友相关联的表,而又不需要其他模型吗?

  1. Can I do a has_and_belongs_to_many relationship with a table that relates user to friends which are users, without incurring an additional model to do so?

是否有一个额外的模型是谨慎的做法,以防以后出现其他要求?

Is it prudent to have an additional model 'in case' additional requirements pop up later?


推荐答案

您正在寻找的是称为,这意味着您可以引用相同的 model 与其他关联:

What you're looking for is something called a self referential join, which means that you can reference the same model with a different association:

#app/models/user.rb
class User < ActiveRecord::Base
   has_and_belongs_to_many :friendships,
      class_name: "User",
      join_table:  :friendships,
      foreign_key: :user_id,
      association_foreign_key: :friend_user_id
end

您必须创建 habtm 连接表以供参考:

You'd have to create a habtm join table to have a reference:

$ rails g migration CreateFriendshipsTable

#db/migrate/create_friendships_table____.rb
class CreateFriendShipsTable < ActiveRecord::Migration
   def change
      create_table :friendships, id: false do |t|
        t.integer :user_id
        t.integer :friend_user_id
      end

      add_index(:friendships, [:user_id, :friend_user_id], :unique => true)
      add_index(:friendships, [:friend_user_id, :user_id], :unique => true)
   end
end

$ rake db:migrate

这里有很多参考资料:

There is a great reference here: Rails: self join scheme with has_and_belongs_to_many?

只有您知道,您才会拥有额外的属性。如果没有,您可以根据需要长时间使用 habtm

Only if you know you're going to have extra attributes. If not, you can get away with the habtm for as long as you want.

这篇关于模型设计:用户拥有的朋友是用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 12:34
查看更多