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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:36:16+00:00 2026-06-04T07:36:16+00:00

I have model User class User < ActiveRecord::Base has_and_belongs_to_many :roles attr_accessible :login, :email, :password,

  • 0

I have model User

class User < ActiveRecord::Base
    has_and_belongs_to_many :roles

    attr_accessible :login, :email, :password, :password_confirmation ...
    attr_accessible :role_ids, :active, :as => :super_admin

    validates :email, :presence => true,
                      :format => {:with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i},
                      :uniqueness => {:case_sensitive => false},
                      :restricted_email_domain => true
    ...
end

and two separate user controllers for front-end and back-end. First one is very trivial and works well, second one is below

class Admin::UsersController < Admin::BaseController
    def create
        @user = User.new
        @user.assign_attributes(params[:user], :as => :super_admin)
        if @user.save
        ...
    end
    def update
        @user = User.find(params[:id])
        if @user.update_attributes(params[:user], :as => :super_admin)
        ...
    end
end

I’m using custom validator that checks whether user’s email domain restricted or not

class RestrictedEmailDomainValidator < ActiveModel::EachValidator
    def validate_each(record, attr_name, value)
        if !value.include?("@") # this value is nil if I add ":as => :super_admin"
            record.errors.add(attr_name, :invalid, options.merge(:value => value))
        else
            domain = value.split("@")[1]
            record.errors.add(attr_name, :restricted_email_domain, options.merge(:value => value)) if ::RestrictedEmailDomain.where(:domain => domain).exists?
        end  
    end
end
module ActiveModel::Validations::HelperMethods
    def validates_restricted_email_domain(*attr_names)
        validates_with RestrictedEmailDomainValidator, _merge_attributes(attr_names)
    end
end

Update action works well but create action gives You have a nil object when you didn't expect it! error in config/initializers/restricted_email_domain_validator.rb:3:in 'validate_each'. Without :as => :super_admin everything is ok, but of course role_ids and active attributes are not assigned.

I can assign values manually, but I think it is not a perfect solution.

  • 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-04T07:36:17+00:00Added an answer on June 4, 2026 at 7:36 am

    I may be wrong, as I don’t have a way to check it now, but I think that when you write

    attr_accessible :role_ids, :active, :as => :super_admin
    
    • only these two attributes are accessible to you when you modify them as super_admin. So
      @user.assign_attributes(params[:user], :as => :super_admin) – only sets these two fields, leaving the rest in default state.

    Did you try something like

    attr_accessible :login, :email, :password, :password_confirmation ...
    attr_accessible :login, :email, :password, :password_confirmation ..., :role_ids, :active, :as => :super_admin
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model: class User < ActiveRecord::Base has_and_belongs_to_many :roles end class Role
I have got this model: class User < ActiveRecord::Base has_and_belongs_to_many :roles before_destroy do |u|
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
I have 3 model class User < ActiveRecord::Base has_many :profiles has_many :companies, :through =>
I have a Post and a Tag model: class Post < ActiveRecord::Base has_and_belongs_to_many :tags
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have a Post model: class Post < ActiveRecord::Base belongs_to :user has_many :taggings, :dependent
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to

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.