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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:07:57+00:00 2026-05-30T02:07:57+00:00

I am currently running into a problem of connecting one model to another. Basically

  • 0

I am currently running into a problem of connecting one model to another. Basically I want a user (user model) to be able to select a specific school (school model) through a form and the user will be under that specific school until they change it. I can’t figure out how to exactly go about doing this, any help will be much appreciated! Thank you!

user model

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :biography, :avatar
  has_secure_password
  belongs_to :school, foreign_key: "school_id"
end

I have nothing defined for anything of school in the user model cause I am not sure what to put.

school model

class School < ActiveRecord::Base
  attr_accessible :name, :school_id

  has_many :users

  validates :school_id, presence: true

end

user controller

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user
  else
    redirect_to current_school
  end
end

def update
 @user = current_user
 if @user.update_attributes(params[:user])
  flash[:notice] = "Successfully updated profile."
  redirect_to home_path
 else
  redirect_to current_school
 end
end

school controller

def create
  school = School.find(params[:name])
   if school
   session[:school_id] = school.id
   redirect_to school_path(school)
  end
end

def show
  @school = School.find(params[:id])
  @user = User.new
end

Form for User with school inside

<%= simple_form_for @user do |f| %>
<%= f.label :Name %></br>
<%= f.text_field :name, :class => 'modal_settingfield' %>
</br></br>
<%= f.label :Email %></br>
<%= f.text_field :email, :class => 'modal_settingfield' %>
</br></br>
<%= f.label :Password %> <span class='faded'>(Leave blank to not change)</span>
<%= f.password_field :password, :class => 'modal_settingfield'%>
</br></br>
<%= f.label :Password_Confirmation %> <span class='faded'>(Leave blank to not change)</span>
<%= f.password_field :password_confirmation, :class => 'modal_settingfield'%>
</br></br>
<%= f.label :School_id %> <span class='faded'>(Leave blank to not change)</span>
<%= f.association :school, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school", :label => false, :required => false %>
<%= f.label :Biography %> <span class='faded'>(Please limit Biography to <span class='TextCounter'></span>letters)</span></br>
<%= f.text_area :biography, :class => 'modal_settingfield'%>
</br></br>
<%= f.label :Photo %> <span class='faded'>(Add a Profile Picture)</span></br>
<%= f.file_field :avatar %>
<%= f.submit 'Update', :class => 'update_button' %>
<% end %>

Current School

def current_school
  @current_school ||= School.find(session[:school_id]) if session[:school_id]
end
helper_method :current_school

Sessions Controller(This is login code)

def create
  user = User.find_by_email(params[:email])
  if user && user.authenticate(params[:password])
    if params[:remember_me]
      cookies.permanent[:auth_token] = user.auth_token
    else
      cookies[:auth_token] = user.auth_token
    end
    redirect_to home_path
  else
    flash.now.alert = "Invalid email or password"
    redirect_to current_school
  end
end

def destroy
  cookies.delete(:auth_token)
  redirect_to current_school
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-05-30T02:07:58+00:00Added an answer on May 30, 2026 at 2:07 am

    I suggest the simple_form gem for this type of thing, and in general for easier, cleaner and less verbose forms. See the section on Collections.

    You’d have code like this:

    <%= simple_form_for @user do |f| %>
      <%= f.input :name %>
      <%= f.input :school_id, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school" %>
      <%= f.button :submit %>
    <% end %>
    

    This assumes that the @schools variable is set by the controller action that renders this form. I’m assuming that these would be School objects, having :name and :id methods.

    Also, your School model should probably not have school_id as attr_accessible, I think you want this on the User model, along with the validation rule. You also don’t need to set the foreign key explicitly if you’re following the conventional naming, i.e.:

    class User < ActiveRecord::Base
      attr_accessible :school_id, :name, :email, :password, :password_confirmation, :biography, :avatar
      has_secure_password
      belongs_to :school
    
      validates :school, presence: true
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I am running into a problem where I am not able to save
I am currently running into a problem where an element is coming back from
I am currently running into an architectural problem in my Python app (using Twisted)
I'm currently running into some issues resizing images using GD. Everything works fine until
I'm currently using Entity Framework and am running into this issue: The relationship between
We are currently developing an application for a msp430 MCU, and are running into
We're currently running a server on Compatibility mode 8 and I want to update
I'm running into a bit of a problem with my WCF service that is
Currently we are working with a user input of time. We want to take
I have migrated nodes using migrate module v2. Currently i am running into a

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.