I am having trouble accessing an a related model's attributes by a foreign key.
Each user has many applications
Each application belongs to one school
When I try to show the school_name attribute in the users dashboard (user#show), I get a undefined method error for school_name
The application has a user_id and a school_id, which should serve as foreign keys. I was thinking there might be a problem with this, but couldn't find documentation.
I can call @application.school_id and it will return the correct integer value, but related attributes on the school table are irretrievable.
Any help would be much appreciated.
Users controller
class UsersController < ApplicationController
before_filter :authenticate_user!
def show
@user = current_user
@applications = @user.applications :include => [:school_name]
end
end
User Model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :applications
has_many :editors, :through => :applications
has_and_belongs_to_many :schools
end
User/show.html.erb
<div class="hero-unit">
<center><h1> Hello <%= @user.name %></h1></center>
<%= link_to "New Application", new_application_path %>
<h2>
Current Applications
</h2>
<%@applications.each do |app|%>
<%= app.school_name %>
<%end%>
</div>
Application Model
require 'carrierwave'
class Application < ActiveRecord::Base
mount_uploader :app_file, ImageUploader
belongs_to :user, :class_name => User, :foreign_key => "user_id"
belongs_to :school, :class_name => School, :foreign_key => "school_id"
belongs_to :editor
end