代码之家  ›  专栏  ›  技术社区  ›  mattC

rails-在模型之间创建一个属于关联会阻止我编辑模型

  •  0
  • mattC  · 技术社区  · 7 年前

    我试图在我的rails应用程序(用户和硬币)中创建两个模型之间的关联,其中硬币属于用户,而用户有很多硬币。当我在coin模型中将belongs_添加到association时,我将无法再编辑或创建coin页面。为什么会这样?一旦删除关联,就可以再次创建/编辑。另外,用户页面上相应的has_many关联也不具有相同的效果。我很感激你能帮助我理解这里发生的一切,以及我怎样才能正确地建立这种联系。谢谢。

    用户地址

    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
    

    科因

    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
    

    硬币控制器.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
    

    用户控制器.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
    

    图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 回复  |  直到 7 年前
        1
  •  5
  •   jvillian    7 年前

    用途:

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

    在轨道5中, belongs_to 默认情况下强制存在关联记录。你需要使用 optional: true 以允许版主ID为零。