Right now, I am in the middle of creating a social media application on Ruby on Rails, I have implemented a 5-point voting system. Where you can vote for news published on the website from 1-5, I would like to know what is the best approach when processing updates in the voting system.
In the example. If the user has already voted in the article, I would like to return the account that he gave in this article and gently block the vote (since I allow only 1 vote per user, and I can change your vote at any time), but if he is not me I will give an article with a vote of 0.
I know a way to do this, I could do it in the view and check if the current user voted for this article, I would send them to the EDIT view otherwise in the SHOW view. (I think)
In any case, what would be the โrightโ approach for this?
EDIT: I forgot to say that the voting field is partial, which I do. Is it possible to just somehow update the part?
EDIT2:
class Article < ActiveRecord::Base
has_many :votes
belongs_to :user
named_scope :voted_by, lambda {|user| {:joins => :votes, :conditions => ["votes.user_id = ?", user]} }
end
class User < ActiveRecord::Base
has_many :articles
has_many :votes, :dependent => :destroy
def can_vote_on?(article)
Article.voted_by(current_user).include?(article)
end
end
source
share