2

I have an STI model structure where super-class is User and sub-classes are Member and NonMember. I want to have only one Member and multiple NonMember for an email. For e.g. with email a@gmail.com there can be only one Member object but at the same time we can have multiple NonMember with that email for different subdomains.

Please let me know how to take care of this as I am stuck on it and I have to tackle this problem very soon.

abhas
  • 5,193
  • 1
  • 32
  • 56

1 Answers1

1

Here is STI relation alongwith email uniqueness validation. I hope it will help !

class User < ActiveRecord::Base
end

class Member < User
 validates uniqueness_of :email
end

class NonMember < User
 validates_uniqueness_of :email, :scope => :subdomain
end
Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48
  • is there any changes I have to do in devise config? – abhas Aug 14 '12 at 09:29
  • yes..you need to remove uniqueness validation from devise... also you need to add `username` as as devise authentication field. – Sandip Ransing Aug 14 '12 at 10:43
  • Ok I understood the first part that I have to overwrite the validations but help me in understanding the second one that why I have to include `username` as an authentication field?? – abhas Aug 14 '12 at 10:46
  • by default devise uses `email` as authentication field while login to system but if you are saying that email will be uniq scoped to subdomain then only first user can able to login to system and others just can't .. hence i think you should have `username` field that will be uniq and can be used to login. – Sandip Ransing Aug 14 '12 at 10:59
  • you can override default authentication field from `email` to `username` inside `config/initializers/devise.rb` – Sandip Ransing Aug 14 '12 at 11:00
  • Ok but I want just `Member` to login which will have unique `email`, don't want `NonMember` to login. Does that mean my requirements will be fulfilled if I keep authentication field as `email`? – abhas Aug 14 '12 at 11:03
  • In that case you can go with `email` as authentication field .. i don't see any problem :) – Sandip Ransing Aug 14 '12 at 11:23