Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:02:37+00:00 2026-06-05T06:02:37+00:00

Basically I have a page that lists 10 most recent microposts. Each post has

  • 0

Basically I have a page that lists 10 most recent microposts. Each post has a like button. When this like button is clicked the likes table in my database is updated.

Likes table:

+----+------------+--------------+-------------------------+-------------------------+---------+
| id | likable_id | likable_type | created_at              | updated_at              | user_id |
+----+------------+--------------+-------------------------+-------------------------+---------+
| 2  | 5770       | Micropost    | 2012-06-09 11:30:55 UTC | 2012-06-09 11:30:55 UTC | 2       |
| 3  | 5770       | Micropost    | 2012-06-09 11:42:45 UTC | 2012-06-09 11:42:45 UTC | 2       |
+----+------------+--------------+-------------------------+-------------------------+---------+

A user must only be able to like a micropost once. I can make this possible with some jquery/js by displaying an unlike button that points to a destroy path when ever a micropost is liked.

But is there a way to do this server side too? Like not allow a micropost to be liked more than once by any means necessary? So if I was to go into rails console and try to manually like a micropost I already liked it wouldn’t work because it would see that I had already liked the micropost?

Like model:

class Like < ActiveRecord::Base
  belongs_to :likable, :polymorphic => true
  attr_accessible :likable_id, :likable_type, :user_id
end

Micropost model:

class Micropost < ActiveRecord::Base
    belongs_to :user
    has_many :likes, :as => :likable
end

Likes controller:

class LikesController < ApplicationController
  def create
    micropost = Micropost.find(params[:micropost])
     like = micropost.likes.build(:user_id => current_user.id)
     like.save
  end
end

Likes form:

<%= form_tag likes_path, :remote => true, :class => "like_micropost" do %>  
   <%= hidden_field_tag :micropost, micropost.id %>
       <%= submit_tag '', :class => "likeMicropostSubmit"  %> 
<% end %>

I previous tried this with no luck:

class LikesController < ApplicationController
  def create
    micropost = Micropost.find(params[:micropost])
    if micropost.likes.where(:user_id => current_user.id).nil?
     like = micropost.likes.build(:user_id => current_user.id)
     like.save
    end
  end
end

Kind regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:02:38+00:00Added an answer on June 5, 2026 at 6:02 am
    # in Like class
    validates_uniqueness_of :user_id, :scope => [:likable_id, :likable_type]
    

    This way you’ll get validation error on trying to like something more than once.

    See docs for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have this webpage that is the header on any page you go
The blog is basically a page that lists the summary of like 10 articles,
I have a page that will basically be used to concatenate a bunch of
Basically I have a single page site that scrolls when the user clicks on
Basically I have a single page on my site that I want any php
I have a query that is more complicated, but basically creates an HTML page
Basically I have a series of projects displayed on the page. Each project consists
I'm building this website and I have basically two questions about this page: http://sites.publishyours.com.br/silviamecozzi/pt/obra/deserto_das_palmas.php
I have a page that displays a list of items. My markup is basically
I have a page that that lists products returned from a mysql query. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.