vendredi 17 août 2018

Need to store the post_id and user_id in read_statuses table using rails

When i initiate the show action the controller want to store the post_id and user_id into the another table.But it need to store for the first occurrence only For eg. (if user 1 opens the post 1 for the first time it creates an entry into the database but not at when he view the same post for the second time) Post controller show cation will be:

def show

@post = @topic.posts.find(params[:id])
@rate=@post.ratings.all
@rate = Rating.where(post_id: @post.id).group("rate").count
@read_status= ReadStatus.create(user_id: current_user,post_id: @post.id)

end

Post model:

class Post < ApplicationRecord
belongs_to :user
has_many :read_statuses
has_many :users,through: :read_statuses
end

User model:

class User < ApplicationRecord
has_many :posts
has_many :read_statuses
has_many :posts, through: :read_statuses
end

Read status model:

class ReadStatus < ApplicationRecord
belongs_to :user
belongs_to :post
end

Aucun commentaire:

Enregistrer un commentaire