title: Rails - Creating a belongs_to association between models prevents me from editing the model
body: <p>I'm trying to create an association between two models in my Rails app (User and Coin) where Coin belongs_to User and User has_many Coins. When I add the belongs_to association in the Coin model, I am no longer able to edit or create Coin pages. Why would it do this? As soon as I remove the association, I'm able to create/edit again. Also, the corresponding has_many association on the User page does not have the same effect. I'd appreciate any help in understanding what is happening here and how I can properly make this association. Thanks.</p>
  
  <p>User.rb </p>
  
  <pre><code>class User &lt; ApplicationRecord
    acts_as_votable
    has_many :questions, dependent: :destroy
    has_many :events, dependent: :destroy
    has_many :links, dependent: :destroy
    has_many :posts, dependent: :destroy
    has_many :moderated_coins, class_name: "Coin"
  
    devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable,
       :validatable, authentication_keys: [:login]
  
    validates :username, presence: :true, uniqueness: { case_sensitive: false }
    validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline =&gt; true
    validate :validate_username
  
    def validate_username
      if User.where(email: username).exists?
        errors.add(:username, :invalid)
      end
    end
  
    def login=(login)
      @login = login
    end
  
    def login
      @login || self.username || self.email
    end
  
    def self.find_for_database_authentication(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value =&gt; login.downcase }]).first
      elsif conditions.has_key?(:username) || conditions.has_key?(:email)
        where(conditions.to_h).first
      end
    end
  end
  </code></pre>
  
  <p>Coin.rb</p>
  
  <pre><code>class Coin &lt; ApplicationRecord
    validates :currency_name, presence: true
    has_many :questions, dependent: :destroy
    has_many :events, dependent: :destroy
    has_many :links, dependent: :destroy
    mount_uploader :picture, PictureUploader
    has_and_belongs_to_many :genres
    # belongs_to :moderator, class_name: "User".  &lt;--- * The problem is here
    validate :picture_size
  
    private
  
      def picture_size
        if picture.size &gt; 5.megabytes
          errors.add(:picture, "Picture must be smalled than 5MB.")
        end
      end
  end
  </code></pre>
  
  <p>coins_controller.rb</p>
  
  <pre><code>class CoinsController &lt; ApplicationController
  
    load_and_authorize_resource param_method: :question_params
    before_action :find_coin, only: [:edit, :update, :destroy ]
    before_action :authenticate_user!, except: [:index, :create, :show]
  
    def index
      @search = Coin.ransack(params[:q])
      @coins = @search.result(distinct: true)
    end
  
    def new
      @coin = Coin.new
    end
  
    def create
      @coin = Coin.new(coin_params)
      if @coin.save
        flash[:success] = "Coin created"
        redirect_to @coin
      else
        render 'new'
      end
    end
  
    def show
      @coin = Coin.find(params[:id])
    end
  
    def edit
      authorize! :update, @coin
    end
  
    def update
      if @coin.update(coin_params)
        redirect_to @coin
      else
        render 'edit'
      end            
    end
  
  def destroy
    Coin.find(params[:id]).destroy
    redirect_to coins_url
  end
  
  private
  
    def coin_params
      params.require(:coin).permit( :currency_name, :currency_abbrev, :moderator_id, :accepted, :picture, :question1, :question2, :question3, :question4, genre_ids:[])
    end
  
    def find_coin
      @coin = Coin.find(params[:id])
    end
  
  end
  </code></pre>
  
  <p>user_controller.rb</p>
  
  <pre><code>class UsersController &lt; ApplicationController
  
    before_action :authenticate_user!
  
    def show
      @user = User.find(params[:id])
      respond_to do |format|
          format.html # show.html.erb
          format.xml { render :xml =&gt; @user }
      end
    end
  end
  </code></pre>
  
  <p>schema.rb</p>
  
  <pre><code>create_table "coins", force: :cascade do |t|
    t.string "link_name"
    t.string "currency_name"
    t.string "currency_abbrev"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "permalink"
    t.boolean "accepted", default: false
    t.datetime "accepted_at"
    t.string "genre"
    t.integer "genre_id"
    t.integer "moderator_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.string "username"
    t.string "wallet"
    t.boolean "admin", default: false
    t.boolean "moderator", default: false
    t.decimal "currentbalance", precision: 8, scale: 2
    t.decimal "payout_to_date", precision: 8, scale: 2
    t.text "bio"
    t.string "link1"
    t.string "link2"
    t.string "link3"
    t.string "link4"
    t.string "link5"
    t.string "name"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end
  </code></pre>