ruby on rails - Uninitialized constant error using a relationship -
ruby on rails - Uninitialized constant error using a relationship -
i followed instructions here create model lesson in there pupil , teacher (both of model user) , lesson start date.
#lesson controller class lesson < activerecord::base belongs_to :student, class_name => 'user' belongs_to :teacher, class_name => 'user' end #user controller class user < activerecord::base has_many :lessons_to_attend, :class_name => 'lesson', :foreign_key => 'student_id' has_many :lessons_to_teach, :class_name => 'lesson', :foreign_key => 'teacher_id' end
the migration went smoothly , on page seek query student's lessons tomorrow:
<% @date = 1.day.from_now %> <%= @date.strftime("%a")%></br> <%= @date.strftime("%-d/%-m/%y")%> <% @user.lessons_to_attend.each |l| %> lesson <% end %>
but when navigate page error uninitialized constant error lesson::user
what did miss out? i'll include user controller in case needs added in there.
class userscontroller < applicationcontroller before_action :set_user, only: [:show, :edit, :update, :destroy] # /users # /users.json def index @users = user.all end # /users/1 # /users/1.json def show @user = user.find(params[:id]) end # /users/new def new @user = user.new end # /users/1/edit def edit end # post /users # post /users.json def create @user = user.new(user_params) respond_to |format| if @user.save format.html { redirect_to @user, notice: 'user created.' } format.json { render :show, status: :created, location: @user } else format.html { render :new } format.json { render json: @user.errors, status: :unprocessable_entity } end end end # patch/put /users/1 # patch/put /users/1.json def update respond_to |format| if @user.update(user_params) format.html { redirect_to @user, notice: 'user updated.' } format.json { render :show, status: :ok, location: @user } else format.html { render :edit } format.json { render json: @user.errors, status: :unprocessable_entity } end end end # delete /users/1 # delete /users/1.json def destroy @user.destroy respond_to |format| format.html { redirect_to users_url, notice: 'user destroyed.' } format.json { head :no_content } end end private # utilize callbacks share mutual setup or constraints between actions. def set_user @user = user.find(params[:id]) end # never trust parameters scary internet, allow white list through. def user_params params[:user] end end
two things:
belongs_to :student, class_name => 'user' belongs_to :teacher, class_name => 'user'
syntax error on class_name. should either :class_name => 'user'
or class_name: 'user'
.
the other thing think need set inverse_of
on both sides of association.
class lesson < activerecord::base belongs_to :student, class_name: 'user', inverse_of: :lessons_to_attend belongs_to :teacher, class_name: 'user', inverse_of: :lessons_to_teach end class user < activerecord::base has_many :lessons_to_attend, class_name: 'lesson', foreign_key: 'student_id', inverse_of: :student has_many :lessons_to_teach, class_name: 'lesson', foreign_key: 'teacher_id', inverse_of: :teacher end
ruby-on-rails ruby ruby-on-rails-4
Comments
Post a Comment