2017-05-11 12 views
0

多対多の関係を持つ3つのテーブルからdevelopment.sqlite3のデータを取得することができません。私は与えられた場所やカテゴリに属する​​コースを手に入れることができますが、それは周囲の道には向いていません。Rails 5.0.2 - 多対多のリレーションシップモデルからデータを取得できません

私はクラスで教えられた方法に従っていましたが、モデルにはいくつかの制約がありませんでした。

誰かが一度見て、私にいくつかの提案をしてください。どうもありがとうございます!

これは私のschema.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 

私は場所や特定のコースに関連付けるカテゴリを取得したいです。それから私は、以下のようなmodels内の関係を追加しました:

# 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 

はその後courses_controller.rbに、私はこのような場所やカテゴリを取得:

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 

は、最後に私はこのようなhtml.erbファイル内のデータを表示します

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

ここにコードをたくさん残して申し訳ありませんが、コードを調べるのに十分な情報を提供する必要があります。

更新:

私はroute.rb

Rails.application.routes.draw do 
    resources :locations 
    resources :categories 
    resources :courses 
end 

でこれを持って、私はすべての人の提案として、コードを編集しているが、それはまだ何も出力しません。私は、クエリが正しいことに気づくが、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]] 
+0

collecionのチェックボックスで、多くの多くのビューを作成することができますか?あなたはカテゴリ/場所を与えられたコースに入れることができませんか?これはRailsのバージョンは何ですか? 'course.categories(course.id)'これは何をすると思われますか?それはちょうど '@ course.categories'でしょうか? – Fallenhero

答えて

0

わからないが、しかし、あなたはおそらく私が連想が働いされるべきだと思うオプションjoin_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 
0

を追加する必要があります。私はそれを自分で試して、それをコンソールで動作させました。おそらくroutingで問題が発生している可能性があります。また

、代わりのビューで:あなたはできる

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

:協会はIDを渡す必要なく、すでにあるので

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

0

簡単にあなたの問題は、正確に何である

<%= 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%> 

関連する問題