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 9045489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:20:20+00:00 2026-06-16T11:20:20+00:00

I did successfully validate duplicated url straight in the model. The code below shows

  • 0

I did successfully validate duplicated url straight in the model. The code below shows that the validation works well when the user creates a new bookmark.

validate :url_cannot_be_duplicated_per_user

def url_cannot_be_duplicated_per_user
  current_user = User.find(self.user_id)
  if current_user.bookmarks.any?{|b| b.url.eql? self.url }
    errors.add(:url, "already added")
  end
end

But the problem is the validation prevents editing a bookmark because when editing it bascially same bookmark, it will go through the model again and catch the duplication. So with that code the update action never happen.

Any idea how to fix it?

PS: I did put a block if else in the controller to check url first before submitting to model. The code goes messy although the validation worked pretty much correctly.

My controller

if duplicated? params[:bookmark][:url]
  flash[:error] = 'This bookmark already added'
  @bookmark = current_user.bookmarks.build(params[:bookmark])
  render 'new'
else
  • 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-16T11:20:23+00:00Added an answer on June 16, 2026 at 11:20 am

    You can validate uniqueness with scope:

    class Bookmark < ActiveRecord::Base
      validates :url, :uniqueness => {:scope => :user_id}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Did actually someone make already a tutorial that shows how to customize an UITableView?
I successfully did an application that uses Flex as a client as GAE as
I did an Android project and run it. It ran successfully. I developed a
I was successful in writing the query that lists salesmen that did sell to
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
I'm creating a Map app in iPhone using MKMapView . I did successfully find
my beaweblogic did not startup successfully. Do you have an idea? Thanks failed to
I am trying to create a user registration page using MVC 3, so that
Did anyone successfully install apgl graph library for python? http://packages.python.org/apgl/ Is there other alternative
I want to download some pages from a website and I did it successfully

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.