活动记录关系显示在查询字符串而不是ID

时间:2015-07-02 18:53:46

标签: ruby-on-rails ruby activerecord

我试图找出为什么我在查询字符串中显示活动记录关系而不是ID号。似乎有一种积极的记录关系正在传递,但我并不完全确定原因。这就是我的查询字符串目前的样子:

http://localhost:3000/schools/2/courses/%23%3CSchool::ActiveRecord_Relation:0x007fac99bfafe8%3E/posts/new

以下是所有相关代码:

路线:

resources :schools do
resources :courses do
  resources :posts
end

课程控制器:

class CoursesController < ApplicationController
    def index
        @courses = Course.all
    end
    def show
        @course = Course.find(params[:id])
        @school = School.where({id: @course[:school_id]})
    end
end

帖子控制器:

class PostsController < ApplicationController
    def new
        @post = Post.new
        @course = Course.find(params[:course_id])
        @school = School.find(params[:school_id])
    end
end

课程show.html.erb文件:

<h1><%= @course.name %></h1>

<p><%= link_to "create new post", new_school_course_post_path(@course,@school)%></p>

模型:

class Course < ActiveRecord::Base
    belongs_to :school
    has_many :user_course
    has_many :users, :through => :user_course
    has_many :posts
    has_many :is_tutors
end

class Post < ActiveRecord::Base
    belongs_to :school
    belongs_to :course
    belongs_to :user
end

class School < ActiveRecord::Base
    has_many :user_school
    has_many :users, :through => :user_school
    has_many :courses
    has_many :posts 
end

提前致谢!

1 个答案:

答案 0 :(得分:0)

中替换此代码

courses_controller.rb

class CoursesController < ApplicationController

  def index
    @courses = Course.all
  end

  def show
    @course = Course.find(params[:id])
    @school = School.where(id: @course.school_id)
  end

end

希望它有所帮助!