【问题标题】:Rails: very elaborate associationsRails:非常复杂的关联
【发布时间】:2014-04-06 13:28:32
【问题描述】:

我的消息系统有很多问题。我很想得到一些帮助。除了 User Message 关系之外,它完全可以工作。我正在尝试将authored_messagesUserreceived_messagesUser 相关联。这样做的障碍是received_messages 应该经过两个不同的模型(ConversationUserConversation),而authored_messages 应该是使用消息的user_id 字段的直接关系。她在这里:

模型

用户:

class User < ActiveRecord::Base

has_many(
:conversation_users, 
inverse_of: :user,
dependent: :destroy
)

has_many(
:conversations, 
through: :conversation_users
)

# has_many :messages   ?

# has_many(            ?
# :received_messages,
# class_name: "Messages",
# through: :conversations,
# source: :conversation_users
# )

消息:

class Message < ActiveRecord::Base

belongs_to :user                               # ?

belongs_to :recipient, class_name: "User"      # ?

belongs_to(
:conversation, 
inverse_of: :messages
)

has_many(
:conversation_users, 
through: :conversation
)

accepts_nested_attributes_for :conversation

对话:

class Conversation < ActiveRecord::Base  

has_many(
:messages, 
inverse_of: :conversation
)

has_many(
:conversation_users, 
inverse_of: :conversation
)

has_many(
:users, 
through: :conversation_users
)

accepts_nested_attributes_for :messages

ConversationUser(连接模型):

class ConversationUser < ActiveRecord::Base

belongs_to(
:user, 
inverse_of: :conversation_users
)

belongs_to(
:conversation, 
inverse_of: :conversation_users
)

has_many(
:messages,
through: :conversation
)

delegate :users, to: :conversation
accepts_nested_attributes_for :conversation

迁移

用户

# None involved with messaging

留言

t.integer :user_id
t.integer :conversation_id
t.text :body

对话

# None, except the :id field. It helps relate messages and conversation_users

对话用户

t.integer :user_id
t.integer :conversation_id

在我看来,我会添加类似这样的内容 (how do I associate one model twice to another),但我很难将其应用到我的模型中。为了进一步阐明这些关联是如何工作的,这是一个非常简化的视觉效果:

users--->conversation_users
  :             |
  :             |
  V             V
messages<---conversation

我真的非常感谢我能得到的任何帮助,希望这个问题能帮助其他处理复杂关联的人!

编辑

我忘了提,但是这个消息系统可以有多少个收件人(conversation_users),就像发件人决定的一样。 (当发件人创建新对话时)

【问题讨论】:

  • 快速提问 - 为什么多行关联有括号?

标签: ruby-on-rails activerecord associations


【解决方案1】:

如何知道消息是sent 还是received

在您的 message 模型中,您仅引用 user_idconversation_id。您通常需要recipient_idsender_id 而不仅仅是user_id

class User < ActiveRecord::Base
    has_many :conversation_users, inverse_of: :user, dependent: :destroy
    has_many :conversations, through: :conversation_users
end

class Conversation < ActiveRecord::Base
    has_many :conversation_users
    has_many :users, through: conversation_users

    has_many :sent_messages, class: "Message", through: :conversations, foreign_key: "sender_id", source: :user
    has_many :received_messages, class: "Message", through: :conversations, foregin_key: "recipient_id", source: :user
end

class Message < ActiveRecord::Base
    belongs_to :conversation
    belongs_to :sender, class_name: "User", primary_key: "sender_id"
    belongs_to :recipient, class_name: "User", primary_key: "recipient_id"
end

messages
t.integer :sender_id
t.integer :recipient_id
t.integer :conversation_id
t.text :body

这应该产生:

@user.conversations[x].sent_messages
@user.conversations[x].received_messages

【讨论】:

  • 消息模型不能有receiver_id 字段,因为它有很多收件人。收件人是所有的conversation_users。每个对话用户都有一个包含许多消息的对话。但我想知道如何通过所有三个关系到消息模型并解析出 user_id 不等于接收用户的消息(通过三个模型返回)
【解决方案2】:

我最终通过进一步的反复试验弄明白了。我让它变得比以前更难了。以下是UserMessage 类的正确更改以及它们之间的关系:

留言

class Message < ActiveRecord::Base

belongs_to :user                # This

belongs_to(
:conversation, 
inverse_of: :messages
)

has_many(
:conversation_users, 
through: :conversation
)

has_many(                       # This
:recipients, 
class_name: "User", 
through: :conversation_users,
source: :user
)

accepts_nested_attributes_for :conversation

用户

class User < ActiveRecord::Base

has_many(
:conversation_users, 
inverse_of: :user,
dependent: :destroy
)

has_many(
:conversations, 
through: :conversation_users
)

has_many :messages               # This

has_many(                        # This
:received_messages, 
class_name: "Message", 
through: :conversations,
source: :messages
)

我用# This 标记的关联是我最初无法连接的四个关系。这个消息系统是一个独特的四模型消息系统,支持(假设)无限的对话用户。它的行为与许多其他社交网络消息系统没有什么不同。它功能齐全,可以从任何其他相关的类对象访问这四个类对象中的任何一个。我希望这个 Q/A 能够为任何试图构建类似消息传递系统的人提供很好的参考。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多