Search code examples
ruby-on-railsdeviseassociationsmodels

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


Originally I had a Joke.rb model that belonged to User model. Joke table had :joke, :author. It worked perfectly when using @joke = current_user.jokes.new(joke_params) but I took the :author out of the Joke model and made it its own model so I can make vanity url's / dynamic routes for each author, when a joke is created by the user. Here's what I have now:

User Model:
has_many :jokes

Joke Model:
belongs_to :user
has_one :author

Author Model:
has_many :jokes

Here's my Schema:

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

And here's my Joke controller:

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

In rails console, when calling Author or Joke, neither are associated with each other.

I've tried so many different things and I can't get it to work. I've followed many answers from semi-related questions here and just through tutorials, but I suck. Please help, thanks! :)

REPO: https://github.com/joshzandman/random-joke


Solution

  • NoMethodError: undefined method jokes' for Class:0x007fbefa5c4160

    You are doing it wrong by calling Author.jokes. You should use joins or includes

    Author.joins(:jokes)
    

    or

    Author.includes(:jokes)
    

    Also, your associations aren't right. You have has_many :jokes in Author which is right but you have has_one :author in Joke which isn't right. Perhaps you should have belongs_to :author. I recommend you to read associations