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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:41:29+00:00 2026-06-12T18:41:29+00:00

I’m importing heaps of student data from an spreadsheet document. Each row of student

  • 0

I’m importing heaps of student data from an spreadsheet document. Each row of student data will represent a new user, however, the possibility of importing an already existing student exists and I want to bypass some of my user validations such as username uniqueness accordingly so that I can build associations for both new and existing records, but only if they’re being imported to the same school.

Thus far I have the following validation setup in my User model:

user.rb

validates_uniqueness_of :username, :unless => :not_unique_to_school?

def not_unique_to_school?
  user = find_by_username(self.username)
  user.present? && user.school_id == 6
end

Now how would I go about replacing that 6 with a value I have access to in the controller? Instructors will be the ones handling the importing and they’ll be importing students to their school so I would typically run current_user.school_id to retrieve the school id that I want them to be imported to, but I don’t have access to the current_user helper in my model.

I’m not concerned about duplicating usernames as I’ll be handling that on a different step, this is just the preliminary validation.


Edit

Simplified school & user model:

user.rb

class User < ActiveRecord::Base    

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, 
                  :first_name, :last_name, :school_id, :roles_mask

  belongs_to :school

  validates_presence_of :username, :on => :create, :message => "can't be blank"
  validates_uniqueness_of :username, :unless => :unique_to_school?

  def unique_to_school?
    user = find_by_username(self.username)
    user.present? && user.school_id == 6
  end 

  def find_by_username(username)
    User.where(:username => username).first
  end     

end

school.rb

class School < ActiveRecord::Base
  attr_accessible :country_id, :name, :state_id

  has_many :users    

end
  • 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-12T18:41:30+00:00Added an answer on June 12, 2026 at 6:41 pm

    Here’s what ended up working for me:

    validate :user_cant_be_duplicate_in_other_schools  
    
    def user_cant_be_duplicate_in_other_schools
        errors.add(:username, :taken) if User.count(:conditions => ["school_id != ? AND username = ?", self.school_id, self.username]) > 0
    end  
    

    As opposed to testing if a User belongs to a particular school we’re testing for the lack of belonging to a particular school. I didn’t come up with this answer, another user posted this as an answer but deleted it shortly after for reasons unknown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

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.