Ruby on rails 像facebook这样的rails中的收件箱消息
我正试图创建一个个人收件箱信息系统,但有几个问题在我脑海中。首先让我解释一下我的系统 这是我的桌子模型Ruby on rails 像facebook这样的rails中的收件箱消息,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我正试图创建一个个人收件箱信息系统,但有几个问题在我脑海中。首先让我解释一下我的系统 这是我的桌子模型 create_table "inboxmessages", :force => true do |t| t.integer "receiver_id" t.integer "sender_id" t.integer "message_id" t.boolean "isread" t.boolean "isstarred" t.
create_table "inboxmessages", :force => true do |t|
t.integer "receiver_id"
t.integer "sender_id"
t.integer "message_id"
t.boolean "isread"
t.boolean "isstarred"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "messages", :force => true do |t|
t.string "subject"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
这种关系将是有如下特点的
inboxmessages
belongs_to :message
belongs_to :user, :class_name => "User", :foreign_key => "sender_id"
belongs_to :user, :class_name => "User", :foreign_key => "receiver_id"
messages
has_many :inboxmessages
user
has_many :inboxmessages
我有他的问题是我不确定如何创建一条允许我有多个用户的消息。这里是我想要的表单的模式
Message.subject
Inboxmessage.receiver # but have multiple different user
------------------------------------------------------------------------
Message.body
Inboxmessage.sender = current_user # hidden field
以下是关于构建此模型/控制器/应用程序的问题
1 - Should my new form be in inboxmessages or messages?
2 - Should I use accept_nested_for or should I use nested resources
3 - Is my model/database is okay or not the best?
4 - Are my foreign_key relationship well define?
提前谢谢 我会这样做:
class User
has_many :mailboxes
has_many :messages, :through => :tags
has_many :tags
end
class Message
has_many :users, :through => :tags
has_many :mailboxes, :through => :tags
has_many :tags
end
class Mailbox
has_many :tags
has_many :messages, :through => :tags
has_many :users, :through => tags
end
class Tag
belongs_to :user
belongs_to :message
belongs_to :mailbox
# this class has "read?", "starred", etc.
end
这样,对于多个用户,消息可以出现在多个邮箱中,并且每个用户都可以有自己的“读取”、“星号”等。如果要确保用户只有一个消息副本,也就是说,对于同一用户,消息不在两个或多个邮箱中,则可以限制逻辑
要改进您的模式,请阅读Rails指南,特别是有关以下关联的内容:
- 属于
- 你有吗
- 你有很多
- 你有很多吗
- 逆
- 1-我的新表单应该是收件箱消息还是消息 我的意见是,如果你有一个类似电子邮件的邮件模型,有一个邮箱(或标签)模型,你将获得最大的好处。要创建新消息,新表单通常位于./messages/new.html.erb中
- 2-我应该使用accept_nested_for还是应该使用嵌套资源
http://guides.rubyonrails.org/routing.html#nested-resources
嵌套属性也很好;请参见此处的API:
一个好的经验法则是只向下嵌套一层,因为在这之后,它变得比它的价值更复杂
- 3-我的模型/数据库是否是最好的
- 4-我的外键关系是否明确
一般来说,是的。请注意,电子邮件令人惊讶地难以建模。一个好的经验法则是索引外键。还可以查看Rails关联的“反向”并使用它;这将有助于提高速度和内存使用。我会让我的课程设置如下
class Inbox
has_many :messages
belongs_to :user
end
class User
has_many :messages
has_one :inbox
end
class Message
belongs_to :user
belongs_to :inbox
has_many recipients, class_name: "User", foreign_key: "recipient_id"
end
为什么要有邮箱表?@Nicholas邮箱表对于组织邮件非常有用;例如,用户可以有多个邮箱,如“收件箱”、“主页”、“工作”、“垃圾邮件”等。这种方法是否适用于单个邮箱应用程序?更像一个消息队列?@Nicholas你的问题没有足够的信息让我回答;我建议你可以将你的问题作为一个新的StackOverflow问题发布得更详细,这样更多的人可以帮助你。谢谢@joelparkerhenderson-以下是答案。非常感谢您的全面回复。