我正在尝试实现以下关系和对象调用
用户可以发送许多消息(user.sent_messages) 消息可以有一个发件人(message.sender)
用户可以收到许多消息(user.received_messages) 消息可以有许多接收者(message.receivers)
我的架构如下所示:
create_table "activities", force: true do |t|
t.integer "sender_id"
t.integer "message_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "receiver_id"
end
create_table "messages", force: true do |t|
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "read", default: false
end
我的模特看起来像这样:
class User < ActiveRecord::Base
has_many :activities, class_name: 'Activity', foreign_key: 'sender_id', dependent: :destroy
has_many :sent_messages, through: :activities
has_many :reverse_activities, class_name: 'Activity', foreign_key: 'receiver_id'
has_many :received_messages, through: :reverse_activities
end
class Activity < ActiveRecord::Base
belongs_to :sent_messages, class_name: 'User'
belongs_to :received_messages, class_name: 'User'
belongs_to :message
end
class Message < ActiveRecord::Base
has_many :activities, foreign_key: 'sender_id'
has_one :sender, through: :activities, foreign_key: 'sender_id', class_name: 'User'
has_many :reverse_activities, foreign_key: 'receiver_id', class_name: 'User'
has_many :receivers, through: :reverse_activities, source: :receiver
end
方法sent_messages&amp; received_messages工作,但他们直接指向User表并返回该用户的详细信息,而不是消息。
我还没有尝试让Message模型正常工作,因为User模型不正确。
谢谢!
感谢这两项建议,我有以下工作
class User < ActiveRecord::Base
has_many :activities, class_name: 'Activity', foreign_key: 'sender_id'
has_many :sent_messages, through: :activities, foreign_key: 'message_id', class_name: 'Message', source: :sender
has_many :reverse_activities, class_name: 'Activity', foreign_key: 'receiver_id'
has_many :received_messages, through: :reverse_activities, foreign_key: 'message_id', class_name: 'Message', source: :receiver
end
class Message < ActiveRecord::Base
has_one :sent_activities, class_name: 'Activity', foreign_key: 'message_id'
has_one :sender, through: :sent_activities, foreign_key: 'sender_id', class_name: 'User'
has_many :receiver_activities, class_name: 'Activity', foreign_key: 'message_id'
has_many :receivers, through: :receiver_activities, foreign_key: 'receiver_id', class_name: 'User'
validates :body, presence: true
end
class Activity < ActiveRecord::Base
belongs_to :sender, class_name: 'User'
belongs_to :receiver, class_name: 'User'
belongs_to :receiver, class_name: 'Message'
belongs_to :sender, class_name: 'Message'
end
因此,我想要的方法正在发挥作用。
现在只是为了让创建动作有效!
答案 0 :(得分:4)
太复杂
你为什么不试试这个:
#app/models/message.rb
Class Message < ActiveRecord::Base
belongs_to :sender, class_name: "User", primary_key: "sender_id"
belongs_to :recipient, class_name: "User", primary_key: "recipient_id"
end
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :sent_messages, class_name: "Message", foreign_key: "sender_id"
has_many :received_messages, class_name: "Message", foreign_key: "recipient_id"
end
users
id | name | email | created_at | updated_at
messages
id | sender_id | recipient_id | title | body | created_at | updated_at
这将允许您加载如下数据:
@message.sender
@message.recipient
@user.sent_messages
@user.received_messages
要保存数据,您可以使用:
#app/controllers/messages_controller.rb
def new
@message = Message.new
end
def create
@message = Message.new(message_params)
end
private
def message_params
params.require(:message).permit(:recipient_id, :title, :body).merge(sender_id: current_user.id)
end
#app/views/messages/new.html.erb (user has to be logged in)
<%= form_for @message do |f| %>
<%= f.collection_select(:recipient_id, User.all, :id, :name) %>
<%= f.text_field :title %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
答案 1 :(得分:0)
模型消息中存在一些错误:
class Message < ActiveRecord::Base
foreign_key不应该是:“message_id”?
has_many :activities, foreign_key: 'sender_id'
foreign_key中的同样问题:'sender_id',以及下面的其他可能的事情
has_one :sender, through: :activities, foreign_key: 'sender_id', class_name: 'User'
has_many :reverse_activities, foreign_key: 'receiver_id', class_name: 'User'
has_many :receivers, through: :reverse_activities, source: :receiver
end
您似乎使用邮件的ID加入其他表的发件人ID并接收ID。
class Activity < ActiveRecord::Base
班级名称为“用户”,因此sent_messages,received_messages返回用户信息
belongs_to :sent_messages, class_name: 'User'
belongs_to :received_messages, class_name: 'User'
belongs_to :message
end
希望有所帮助。