Skip to content

Instantly share code, notes, and snippets.

@EminenceHC
Last active December 25, 2015 00:29
Show Gist options
  • Save EminenceHC/6888416 to your computer and use it in GitHub Desktop.
Save EminenceHC/6888416 to your computer and use it in GitHub Desktop.
ActiveRecord::EagerLoadPolymorphicError at /students
Can not eagerly load the polymorphic association :loginable
<% @table.each do |s| %>
<div class="span12">
<%= form_tag(students_path, :method=>'get') do %>
<%= select_tag 'id', options_for_select(@users_array_id), { :prompt => 'Student'} %>
<%= select_tag 'program', options_for_select(@users_array_program), { :prompt => 'Program'} %>
<%= submit_tag 'Submit', :class => 'btn btn-primary' %>
<% end %>
<table class="table table-condensed table-bordered table-striped">
<tr>
<th>ID</th>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>UID</th>
<th>Email</th>
<th>Social</th>
<th>Sex</th>
<th>DOB</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Home</th>
<th>Work</th>
<th>Mobile</th>
<th>Case No</th>
<th>Program</th>
<th>Type</th>
<th>Edit</th>
</tr>
</thead>
<% @p.each do |s| %>
<tbody>
<tr>
<td><%= s.loginable.id %></td>
<td><%= s.first_name %></td>
<td><%= s.middle_name %></td>
<td><%= s.last_name %></td>
<td><%= s.id %></td>
<td><%= s.email %></td>
<td><%= s.social_security_no %></td>
<td><%= s.sex %></td>
<td><%= s.date_of_birth %></td>
<td><%= s.street_address %></td>
<td><%= s.city %></td>
<td><%= s.state %></td>
<td><%= s.postal_code %></td>
<td><%= s.home_phone %></td>
<td><%= s.work_phone %></td>
<td><%= s.mobile_phone %></td>
<td><%= s.loginable.case_no %></td>
<td><%= s.loginable.program %></td>
<td><%= s.loginable_type %></td>
<td><%= link_to "Edit", edit_student_path(@student, id: s.loginable.id) %></td>
</tr>
</tbody>
<% end %>
</table>
</div>
class Student < ActiveRecord::Base
has_one :user, as: :loginable, dependent: :destroy
has_many :drug_assessments, dependent: :destroy
accepts_nested_attributes_for :user
end
class StudentsController < ApplicationController
def create
@student = Student.new(student_params)
@user = User.new(user_params)
@student.save!
@user.loginable = @student
@user.save!
redirect_to root_url
end
def new
@student = Student.new
@user = @student.build_user
end
def edit
@student = Student.find(params[:id])
@user = @student.user
end
def update
#authorize! :update, @student, :message => 'Not authorized as an administrator.'
@student = Student.find(params[:id])
@user = User.find(@student.user.id)
@student.update(student_params)
@user.update(user_params)
if @student.update(student_params) || @user.update(user_params)
redirect_to students_path, :notice => "Student updated."
else
redirect_to students_path, :alert => "Unable to update student."
end
end
def index
@users_array_id = User.where(loginable_type: 'Student').includes(:loginable).all.map { |u| [u.name, u.loginable.id] }
@users_array_program = User.where(loginable_type: 'Student').includes(:loginable).all.map { |u| [u.loginable.program, u.loginable.program] }
#if
#params[:id].present?
# @sid = params[:id]
#else
# @sid = 3
#end
if
params[:program].present?
@program = params[:program]
else
@program = 'Web'
end
@p = User.includes(:loginable).where(loginable_type: 'Student').where(:loginable => {program: 'Web'})
#.where(:loginable => {program: 'Web'})
#.where("students.program = 'Web'")
end
private
def student_params
params.require(:student).permit(:case_no, :program, :admission_status, :temporary_password, :admission_date)
end
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :name, :first_name, :middle_name, :last_name, :social_security_no, :sex, :date_of_birth, :street_address, :city, :state, :postal_code, :home_phone, :work_phone, :mobile_phone, :message_id, :password_confirmation)
end
end
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :loginable, polymorphic: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment