Ruby on rails Rails 5.0.2-无法从许多关系模型中获取数据

Ruby on rails Rails 5.0.2-无法从许多关系模型中获取数据,ruby-on-rails,Ruby On Rails,我一直在从3个具有多对多关系的表中获取development.sqlite3中的数据。我可以得到属于某一特定地点或类别的课程,但这并不奏效 我遵循了课堂上的教学方法,但我认为这些模型可能遗漏了一些约束条件 请大家看一下,给我一些建议。非常感谢你 这是我的模式。rb: create_table "categories", force: :cascade do |t| t.string "title" end create_table "categories_courses", for

我一直在从3个具有多对多关系的表中获取
development.sqlite3
中的数据。我可以得到属于某一特定地点或类别的课程,但这并不奏效

我遵循了课堂上的教学方法,但我认为这些模型可能遗漏了一些约束条件

请大家看一下,给我一些建议。非常感谢你

这是我的
模式。rb

create_table "categories", force: :cascade do |t|
    t.string   "title"
end

create_table "categories_courses", force: :cascade do |t|
    t.integer "course_id",   null: false
    t.integer "category_id", null: false
end

create_table "courses", force: :cascade do |t|
    t.string   "title"
end

create_table "courses_locations", force: :cascade do |t|
    t.integer "course_id",   null: false
    t.integer "location_id", null: false
end

create_table "locations", force: :cascade do |t|
    t.string   "address"
end
我想获得与给定课程相关的位置或类别。然后我在
模型中添加了关系,如下所示:

# Category model
class Category < ApplicationRecord
    has_and_belongs_to_many :courses, join_table: 'categories_courses'
end

# Location model
class Location < ApplicationRecord
    has_and_belongs_to_many :courses, join_table: 'courses_locations'
end

# Course model
class Course < ApplicationRecord
    has_and_belongs_to_many :locations, join_table: 'courses_locations'
    has_and_belongs_to_many :categories, join_table: 'categories_courses'
end
最后,我在
html.erb
文件中显示数据,如下所示:

class CoursesController < ApplicationController
    before_action :set_course, only: [:show, :edit, :update, :destroy]

    def categories
        @course = Course.find params[:id]
        @categories = @course.categories
    end

    def locations
        @course = Course.find params[:id]
        @locations = @course.locations
    end

    private

    def set_course
        @course = Course.find(params[:id])
    end
end
<% course.categories.each {|category| puts category.title + ' '} %>
<% course.locations.each {|location| puts location.address + ' '} %>
我已经按照大家的建议编辑了代码,但仍然没有打印任何内容。我注意到查询是正确的,但在
rails s
控制台中有一个警告:

User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 35], ["LIMIT", 1]]
DEPRECATION WARNING: Passing an argument to force an association to reload is now deprecated and will be removed in Rails 5.1. Please call `reload` on the result collection proxy instead. (called from block in _app_views_courses_index_html_erb___1034719630_104325280 at c:/Users/lovea/RubymineProjects/YourCourse/app/views/courses/index.html.erb:41)
Category Load (0.0ms)  SELECT "categories".* FROM "categories" INNER JOIN "categories_courses" ON "categories"."id" = "categories_courses"."category_id" WHERE "categories_courses"."course_id" = ?  [["course_id", 4]]

不确定,但您可能需要添加选项
join\u table

# Category model
class Category < ApplicationRecord
    has_and_belongs_to_many :courses, join_table: "categories_courses"
end

# Location model
class Location < ApplicationRecord
    has_and_belongs_to_many :courses, join_table: "courses_locations"
end

# Course model
class Course < ApplicationRecord
    has_and_belongs_to_many :locations, join_table: "courses_locations"
    has_and_belongs_to_many :categories, join_table: "categories_courses"
end
#类别模型
类别<应用程序记录
是否有和属于多个:课程,加入表格:“类别课程”
结束
#位置模型
类位置<应用程序记录
是否有和属于多个:课程,加入表格:“课程位置”
结束
#课程模式
班级课程<申请记录
是否有和属于多个:地点,加入表格:“课程地点”
是否有和属于多个:类别,加入表格:“类别课程”
结束

我认为这些关联应该起作用。我自己也试过,并在控制台中运行。这可能是您在路由方面遇到的问题

此外,在视图中,不是:

<% course.categories(course.id).each {|category| puts category.title + ' '} %>
<% course.locations(course.id).each {|location| puts location.address + ' '} %>

你可以:

<% course.categories.each {|category| puts category.title + ' '} %>
<% course.locations.each {|location| puts location.address + ' '} %>


因为关联已经存在,无需传递id。

您可以使用collecion复选框轻松为多对多创建视图

<%= f.collection_check_boxes(:course_id, Course.all, :id, :title) do |b| %>


   <div class="collection-check-box">
     <%= b.check_box %>
     <%= b.label %>

     <%end%>

     <%= f.submit 'add course to location', class: "btn btn-primary" %>

<%end%>


你到底有什么问题?您无法获取给定课程的类别/位置?这是什么Rails版本<代码>课程.categories(course.id)
这应该做什么?它不应该仅仅是
@course.categories
<%= f.collection_check_boxes(:course_id, Course.all, :id, :title) do |b| %>


   <div class="collection-check-box">
     <%= b.check_box %>
     <%= b.label %>

     <%end%>

     <%= f.submit 'add course to location', class: "btn btn-primary" %>

<%end%>