Rails - 在模型之间创建belongs_to关联阻止我编辑模型

时间:2018-06-06 06:12:13

标签: ruby-on-rails

我试图在我的Rails应用程序(用户和硬币)中创建两个模型之间的关联,其中Coin属于用户和用户has_many硬币。当我在Coin模型中添加belongs_to关联时,我无法再编辑或创建Coin页面。它为什么要这样做?一旦删除关联,我就可以再次创建/编辑。此外,“用户”页面上相应的has_many关联不具有相同的效果。我非常感谢您帮助理解这里发生的事情以及如何正确地建立这种联系。感谢。

User.rb

class User < 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 => 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 => login.downcase }]).first
    elsif conditions.has_key?(:username) || conditions.has_key?(:email)
      where(conditions.to_h).first
    end
  end
end

Coin.rb

class Coin < 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".  <--- * The problem is here
  validate :picture_size

  private

    def picture_size
      if picture.size > 5.megabytes
        errors.add(:picture, "Picture must be smalled than 5MB.")
      end
    end
end

coins_controller.rb

class CoinsController < 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

user_controller.rb

class UsersController < 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 => @user }
    end
  end
end

schema.rb

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

1 个答案:

答案 0 :(得分:5)

使用:

belongs_to :moderator, class_name: "User", optional: true

在rails 5中,belongs_to默认强制存在关联记录。您需要使用optional: true才能使moderator_id为零。