Rails:非常复杂的关联

时间:2014-04-04 19:50:09

标签: ruby-on-rails activerecord associations

我的邮件系统存在很多问题。我会喜欢一些帮助。它完全有效,但User Message关系除外。我试图将authored_messagesUserreceived_messagesUser联系起来。这样做的障碍是received_messages应该通过两个不同的模型(ConversationUserConversation),而authored_messages应该是使用消息&#39的直接关系; s 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

ConversationUser

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)。 (当发件人创建新会话时)

2 个答案:

答案 0 :(得分:0)

您如何知道邮件是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

答案 1 :(得分:0)

我最终通过进一步的试验和错误搞清楚了。我做得比以前困难得多。以下是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将成为任何试图构建类似消息传递系统的人的一个很好的参考。干杯!