在 Rails 中关联 Post、Comment、User 和 Vote 模型的有效方法是什么? [英] What's an efficient way of associating Post, Comment, User and Vote models in Rails?
问题描述
现在,我有三个模型 Post、Comment 和 User(使用 Devise),关联如下:
Right now, I have three models Post, Comment and User (using Devise) associated as follows:
post.rb:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :total_votes
validates :title, :presence => true,
:length => { :maximum => 30 },
:uniqueness => true
validates :content, :presence => true,
:uniqueness => true
belongs_to :user
has_many :comments, :dependent => :destroy
end
comment.rb:
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
end
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
validates_presence_of :username
has_many :posts, :dependent => :destroy
has_many :comments, :dependent => :destroy
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else # Create a user with a stub password.
User.create!(:email => data.email, :password => Devise.friendly_token[0,20])
end
end
end
我想添加第四个名为 Vote 的模型,条件如下:
I want to add a fourth model called Vote with the following conditions:
- 帖子和评论都可以投票(向上和向下)并显示总数.
- 每个帖子都会有很多票(上下)并显示总数/总和.
- 每个评论都会有很多票
- 每次投票时都应存储用户的 ID,以便我可以限制每个用户投一票并显示投票用户的 ID/姓名(不确定将其存储在哪里)
- Both posts and comments can be voted (up and down) and show the total/sum.
- Each post will have many votes (up and down) and show the total/sum.
- Each comment will have many votes
- The ID of the user should be stored each time he or she votes so I can restrict one vote per user and show the ID/name of the users who voted (not sure where to store it)
现在,我不确定这是否是使用多态关联和/或计数器缓存的好时机.
Now, I'm not sure if this is a good occasion to use polymorphic associations and/or counter cache.
将这些帖子、评论、用户和投票模型关联起来的有效方法是什么?(如果可能,我想看看迁移的样子)
What's an efficient way of associating these Post, Comment, User and Voting models? (If possible, I would like to see how the migration would look like)
推荐答案
这是一个完美的教科书示例,说明了 多态关联会很有用.
This is a perfect textbook example of where a polymorphic association would be useful.
您的 votes
表迁移如下所示:
Your votes
table migration would look like this:
create_table :votes do |t|
t.references :votable, :polymorphic => true
t.references :user
t.integer :polarity
t.integer :total
end
这将创建一个具有此架构的表:
This would create a table with this schema:
id INTEGER
votable_id INTEGER
votable_type VARCHAR
user_id INTEGER
polarity INTEGER
total INTEGER
这里,user_id
将是投票者,polarity
将是1"表示赞成或-1"表示反对(这让您只需将极性相加即可获得赞成票和反对票以取消),votable_type
将包含投票的内容(Post
或 Comment
),votable_id
将包含投票对象的 id,total
将保留投票总和的运行总数(为了效率).
Here, user_id
would be the person who cast the vote, polarity
would be either '1' for an upvote or '-1' for a downvote (this lets you just sum the polarities to get upvotes and downvotes to cancel), votable_type
would contain what the vote is for (Post
or Comment
), votable_id
would contain the id of the thing the vote is for, and total
would keep a running total of the vote sum (for efficiency).
那么您的模型将如下所示:
Then your models would look like this:
class Vote < ActiveRecord::Base
belongs_to :votable, :polymorphic => true
belongs_to :user
before_create :update_total
protected
def update_total
self.total ||= 0
self.total += self.polarity
end
end
class Post < ActiveRecord::Base
has_many :votes, :as => :votable
end
class Comment < ActiveRecord::Base
has_many :votes, :as => :votable
end
class User < ActiveRecord::Base
has_many :votes
end
这篇关于在 Rails 中关联 Post、Comment、User 和 Vote 模型的有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!