对用户之间的消息建模
我想对用户之间的消息进行建模,要求如下:
用户已接收和发送消息,并且应该由
user.received_messages
和user.sent_messages
检索< /p>消息有发送者和接收者,应通过
message.sender
和message.receiver
。
我已将用户模型创建为:
script/generate model User name:string
并将消息模型创建为:
script/generate model Message content:text sender_id:integer receiver_id:integer
我想出了如下所示的消息,它按预期工作,
class Message < ActiveRecord::Base
belongs_to :sender, :class_name=>'User', :foreign_key=>'sender_id'
belongs_to :receiver, :class_name=>'User', :foreign_key=>'receiver_id'
end
但我不知道如何对用户进行建模,任何建议都会受到赞赏。
I want to model messages between users, here is the requirement:
User has received and sent messages, and should be retrieved by
user.received_messages
anduser.sent_messages
Message has sender and receiver, and should be retrieved by
message.sender
andmessage.receiver
.
I have created the User model as:
script/generate model User name:string
and Message model as:
script/generate model Message content:text sender_id:integer receiver_id:integer
I have come up with the Message like below, and it works as wish
class Message < ActiveRecord::Base
belongs_to :sender, :class_name=>'User', :foreign_key=>'sender_id'
belongs_to :receiver, :class_name=>'User', :foreign_key=>'receiver_id'
end
but I don't know how to model the User, any advise is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像消息模型中一样:
Just as in the Message model:
@eric2323223
如果您只是从米兰复制/粘贴代码,那么我怀疑您看到的错误是因为“foreign_key”前面缺少分号。
这些行应为:
肯尼
@eric2323223
If you just copy/pasted the code from Milan, then I suspect the error you're seeing is because there's a missing semi-colon in front of 'foreign_key'.
The lines should read:
Kenny