Ruby on rails 新职位的通知
我正在开发一个rails应用程序,用户可以在其中发帖,一定很喜欢facebook。我想实现一个通知系统,提醒用户新的帖子。然而,我很难判断用户是否浏览过帖子。我真的是不知所措 我正在使用Desive gem,它使我能够访问某些用户统计数据(如果这有帮助): 还有我的帖子模型:Ruby on rails 新职位的通知,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在开发一个rails应用程序,用户可以在其中发帖,一定很喜欢facebook。我想实现一个通知系统,提醒用户新的帖子。然而,我很难判断用户是否浏览过帖子。我真的是不知所措 我正在使用Desive gem,它使我能够访问某些用户统计数据(如果这有帮助): 还有我的帖子模型: create_table "posts", :force => true do |t| t.integer "user_id" t.text "content" t.dateti
create_table "posts", :force => true do |t|
t.integer "user_id"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
如何实现一个知道用户是否看到帖子的系统?简单的做法如下: 创建一个名为Seen的模型
rails g model Seen post:references user:references
型号/seen.rb
belongs_to :user
belongs_to :post
has_many :seens
has_many :seen_posts, through: :seens, source: :post
has_many :seens
has_many :seen_users, through: :seens, source: :user
def seen_by?(user)
seen_user_ids.include?(user.id)
end
def show
@post = Post.find(params[:id])
current_user.seen_posts << @post unless @post.seen_by?(current_user)
end
型号/用户.rb
belongs_to :user
belongs_to :post
has_many :seens
has_many :seen_posts, through: :seens, source: :post
has_many :seens
has_many :seen_users, through: :seens, source: :user
def seen_by?(user)
seen_user_ids.include?(user.id)
end
def show
@post = Post.find(params[:id])
current_user.seen_posts << @post unless @post.seen_by?(current_user)
end
型号/post.rb
belongs_to :user
belongs_to :post
has_many :seens
has_many :seen_posts, through: :seens, source: :post
has_many :seens
has_many :seen_users, through: :seens, source: :user
def seen_by?(user)
seen_user_ids.include?(user.id)
end
def show
@post = Post.find(params[:id])
current_user.seen_posts << @post unless @post.seen_by?(current_user)
end
你可以创建一个类似这样的方法
型号/post.rb
belongs_to :user
belongs_to :post
has_many :seens
has_many :seen_posts, through: :seens, source: :post
has_many :seens
has_many :seen_users, through: :seens, source: :user
def seen_by?(user)
seen_user_ids.include?(user.id)
end
def show
@post = Post.find(params[:id])
current_user.seen_posts << @post unless @post.seen_by?(current_user)
end
controller/posts\u controller.rb
belongs_to :user
belongs_to :post
has_many :seens
has_many :seen_posts, through: :seens, source: :post
has_many :seens
has_many :seen_users, through: :seens, source: :user
def seen_by?(user)
seen_user_ids.include?(user.id)
end
def show
@post = Post.find(params[:id])
current_user.seen_posts << @post unless @post.seen_by?(current_user)
end
def显示
@post=post.find(参数[:id])
current_user.seed_帖子如果我每隔一分钟左右对此进行一次民意调查,这会让数据库变得非常密集,对吗?我从来没有这样做过,所以我真的不知道。但是,我想真的没有其他方法来跟踪用户浏览的帖子。