0

i'm having problem in checking validates_uniqueness_of which has two condtion.

Class name is InviteGuest

class InviteGuest < ActiveRecord::Base
  attr_accessible :invite_id, :email, :first_name, :last_name, :random_no

  validates_presence_of :invite_id, :unless => :random_no?

  validates_uniqueness_of :email, :scope => [:invite_id] 

Here i'm checking invite_id or :random number for presence. so here my :email feilds are entering first with unique :random_no .which works fine.and i;m validates_uniqueness_of :email, :scope => [:invite_id] but it's not allowing me to enter same :email for different :random_no. i have to put or condition. it's cheking for :invite_id but i would like to check it for :random_no if :invite_id is not present. but it's not allowing me to enter :email for different :random_no.

Any idea??

Akki209
  • 123
  • 12
  • 1
    That was quite hard to follow, could you add some more context about what you're goal is. – Matt May 23 '14 at 12:19

1 Answers1

1

It sounds to like your scope is not set correctly. You should scope on random_no:

validates_uniqueness_of :email, scope: [:invite_id, :random_no]
Mohamad
  • 34,731
  • 32
  • 140
  • 219
  • but dosent that one will check unique email for both values i mean AND relation?? i would like to check for invite_id if present, and if not then check for random_no for restricting same email. but above code not allowing me to check unique email for invite_id which is very staight forward code. But i'm confuse at validates_presence_of :invite_id, :unless => :random_no? Dose this code automitcaly check for :random_no if :invite_id is nor present?? because its not. It's giving me validation error coz there is no :invite_id and same same :email is trying to insert with diff :random_no. – Akki209 May 25 '14 at 18:09