2012-09-07 2 views
2

사용자가 게시 할 수있는 레일 앱을 개발 중이며 페이스 북을 좋아해야합니다. 새 게시물을 사용자에게 알리는 알림 시스템을 구현하고 싶습니다. 그러나 사용자가 게시물을 보았는지 여부를 알려주는 방법에 문제가 있습니다. 나는 말 그대로 우둔한 사람입니다.레일스 새 게시물 알림

I (이 도움이된다면) 나에게 특정 사용자 통계에 액세스 할 수 있습니다 보석 고안 사용하고 있습니다 :

create_table "users", :force => true do |t| 
    t.string "email",     :default => "", :null => false 
    t.string "encrypted_password",  :default => "", :null => false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   :default => 0 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.string "confirmation_token" 
    t.datetime "confirmed_at" 
    t.datetime "confirmation_sent_at" 
    t.string "unconfirmed_email" 
    t.integer "failed_attempts",  :default => 0 
    t.string "unlock_token" 
    t.datetime "locked_at" 
    t.string "authentication_token" 
    t.datetime "created_at",        :null => false 
    t.datetime "updated_at",        :null => false 
    t.string "username",    :default => "", :null => false 
    t.integer "admin",     :default => 0 
    end 

을 그리고 내 게시물 모델 : 내가 알고있는 시스템을 구현할 수있는 방법

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 

사용자가 게시물을 보았는지 여부

답변

3

간단한 aproach 그렇게 될 것이다 :

라는 모델을 생성

rails g model Seen post:references user:references 

모델/seen.rb

belongs_to :user 
belongs_to :post 

모델/user.rb

has_many :seens 
has_many :seen_posts, through: :seens, source: :post 

모델/당신이

모델과 같은 방법 뭔가를 만들 수 있습니다

has_many :seens 
has_many :seen_users, through: :seens, source: :user 

및 post.rb/post.rb

def seen_by?(user) 
    seen_user_ids.include?(user.id) 
end 

컨트롤러/posts_controller.rb

def show 
    @post = Post.find(params[:id]) 
    current_user.seen_posts << @post unless @post.seen_by?(current_user) 
end 
+0

매분마다 이것을 폴링하면 꽤 데이터베이스 집약적입니다. 나는 이것을 결코 한 적이 없기 때문에 나는 정말로 모른다. 그러나 실제로 사용자가 본 게시물을 추적 할 수있는 다른 방법은 없다고 생각합니다. – flyingarmadillo

관련 문제