找不到关联:模型

时间:2017-05-26 07:31:50

标签: ruby-on-rails model associations ruby-on-rails-5 model-associations

我失去了很多睡眠!似乎无法弄清楚为什么协会不在那里......任何帮助都很受欢迎

  

无法找到关联:模型中的catorizations问题

第一行是错误指向的地方

<% if @question.categories.present? %> Categories (<%= @question.categories.count %>): <%= @question.categories.map(&:title).join(", ") %> <% end %>

模型

class Question < ActiveRecord::Base
    belongs_to :user
    has_many :answers, dependent: :destroy
    has_many :categorizations, dependent: :destroy
    has_many :categories, through: :catorizations
end




class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :question
end



class Category < ActiveRecord::Base
  has_many :categorizations, dependent: :destroy
  has_many :questions, through: :categorizations
end




class Answer < ActiveRecord::Base
  belongs_to :question

  validates_presence_of :body

  scope :ordered_by_creation, -> { order("created_at DESC")}
end

QuestionController

class QuestionsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show, :home]
  before_action :find_question, only: [:edit, :update, :destroy, :vote]

  # GET /questions
  # GET /questions.json
  def index
    if signed_in?
      # @question = Question.find(params[:question_id] || params[:id])
      @questions = Question.all
    else
      respond_to do |format|
        format.html { redirect_to root_path, notice: 'Must Sign In To Start Having Fun.' }
        format.json { head :no_content }
        end
    end
  end

  def home

  end
  # GET /questions/1
  # GET /questions/1.json
  def show
    @question = Question.find(params[:question_id] || params[:id])
    @answer = Answer.new
    # @answers = @qustion.answers.ordered_by_creation
  end

  # GET /questions/new
  def new
    @question = Question.new

  end

  def vote
    value = params[:type] == "up" ? 1 : -1
    @question = Question.find(params[:id])
    @question.add_or_update_evaluation(:votes, value, current_user)
    redirect_to :back, notice: "Thank you for voting!"
  end
  # GET /questions/1/edit
  def edit
      @question = Question.find(params[:id])
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = current_user.questions.new(question_params)
    # @question = Question.new(question_params)
    respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: 'Question was successfully created.' }
        format.json { render :show, status: :created, location: @question }
      else
        format.html { render :new }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /questions/1
  # PATCH/PUT /questions/1.json
  def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question was successfully updated.' }
        format.json { render :show, status: :ok, location: @question }
      else
        format.html { render :edit }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end

    def find_question
      @question = current_user.questions.find_by_id(params[:id])
      redirect_to root_path, alert: "Access Denied" unless @question
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:question, :question_id, :vote, {category_ids: []})
    end

end

最后我的架构如果有帮助

模式

  create_table "answers", force: :cascade do |t|
    t.text     "body"
    t.integer  "question_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
  end

  add_index "answers", ["question_id"], name: "index_answers_on_question_id"
  add_index "answers", ["user_id"], name: "index_answers_on_user_id"

  create_table "categories", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "categorizations", force: :cascade do |t|
    t.integer  "category_id"
    t.integer  "question_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_index "categorizations", ["category_id"], name: "index_categorizations_on_category_id"
  add_index "categorizations", ["question_id"], name: "index_categorizations_on_question_id"

  create_table "questions", force: :cascade do |t|
    t.string   "question"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "question_id"
    t.integer  "user_id"
  end

1 个答案:

答案 0 :(得分:2)

  

无法找到关联:模型中的catorizations问题

Question型号

中有拼写错误

has_many :categories, through: :catorizations

应该是

has_many :categories, through: :categorizations