1

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
4

1 に答える 1

2

次の方法で検証できuniquenessますscope

class Bookmark < ActiveRecord::Base
  validates :url, :uniqueness => {:scope => :user_id}
end
于 2012-12-30T02:52:04.583 に答える