使用 Rails 5 + Devise,我无法让模型相互关联

Using Rails 5 + Devise, I can't get the models to associate with each other

最初我有一个属于 User 模型的 Joke.rb 模型。笑话 table 有 :joke, :author。它在使用 @joke = current_user.jokes.new(joke_params) 时完美运行,但我将 :author 从 Joke 模型中取出并使其成为自己的模型,这样我就可以为每个作者制作虚荣 url 的/动态路线,当一个笑话是由用户创建。这是我现在拥有的:

用户模型:
has_many :jokes

笑话模型:
belongs_to :user
has_one :author

作者模型:
has_many :jokes

这是我的架构:

ActiveRecord::Schema.define(version: 20170703173447) do

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

  create_table "jokes", force: :cascade do |t|
    t.string   "joke"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

这是我的笑话控制器:

class JokesController < ApplicationController
  before_action :set_joke, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index]

  def index
    @joke = Joke.order("Random()").first
  end

  def my_jokes
    @jokes = current_user.jokes.all
  end

  def new
    @joke = current_user.jokes.new
  end

  def create
    @joke = current_user.jokes.new(joke_params)
    if @joke.save
      redirect_to root_path, notice: 'Joke was successfully added!'
    else
      render :new
    end
  end

  def update
    if @joke.update(joke_params)
      redirect_to root_path, notice: 'Joke was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @joke.destroy
    redirect_to my_jokes_path, notice: 'Joke was successfully deleted.'
  end

  def edit
  end

  def about
  end

  private

  def joke_params
    params.require(:joke).permit(:joke, :name)
  end

  def set_joke
    @joke = Joke.find(params[:id])
  end

end

在rails控制台中,调用Author或Joke时,两者都没有关联。

我已经尝试了很多不同的东西,但我无法让它工作。我在这里以及通过教程已经从半相关问题中得到了许多答案,但我很烂。请帮助,谢谢! :)

回购: https://github.com/joshzandman/random-joke

NoMethodError: undefined method jokes' for Class:0x007fbefa5c4160

你调用 Author.jokes 是错误的。您应该使用 joinsincludes

Author.joins(:jokes)

Author.includes(:jokes)

另外,你的联想也不对Author 中的 has_many :jokes 是正确的,但 Joke 中的 has_one :author 不正确的 。也许你应该 belongs_to :author。我推荐你阅读 associations