简体   繁体   中英

Rails Associations: Going from has_many to has_one

I'm still new to Rails and am try to figure out how to associate a model with a user as a has_one.

This was set up initially assuming that my users would have many finances. Now, I need to change it so a user has one finance.

Finance model :

class Finance < ActiveRecord::Base
    belongs_to :user
    validates :age, presence: true
    validates :zip, presence: true
end

User model :

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :finances
end

When I change has_many :finances to has_one :finances, my finances controller is throwing an error.

class FinancesController < ApplicationController    
  # GET /finances
  # GET /finances.json
  def index
    @finances = Finance.find_all_by_user_id current_user[:id] if current_user
  end

  # GET /finances/new
  def new
    @finance = current_user.finances.build
  end

  # GET /finances/1/edit
  def edit
  end

  # POST /finances
  # POST /finances.json
  def create
    @finance = current_user.finances.build(finance_params)

    respond_to do |format|
      if @finance.save
        format.html { redirect_to @finance, notice: 'Successfully created.' }
        format.json { render action: 'show', status: :created, location: @finance }
      else
        format.html { render action: 'new' }
        format.json { render json: @finance.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /finances/1
  # PATCH/PUT /finances/1.json
  def update
    respond_to do |format|
      if @finance.update(finance_params)
        format.html { redirect_to @finance, notice: 'Successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @finance.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /finances/1
  # DELETE /finances/1.json
  def destroy
    @finance.destroy
    respond_to do |format|
      format.html { redirect_to finances_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_finance
      @finance = Finance.find(params[:id])
    end

    def correct_user
        @finance = current_user.finances.find_by(id: params[:id])
        redirect_to root_path, notice: "Welcome to TrustRose" if @finance.nil?
    end
end

The error comes here:

 @finance = current_user.finances.find_by(id: params[:id])

I believe this will need to change, but I am not how to query the database to find the finance associated with the user now that the user only has one.

You problem relates to Active Record Pluralisation of your model names: http://guides.rubyonrails.org/association_basics.html

Try this:

@finance = current_user.finance

In your routes.rb , change resources :finances to resource :finance

Then, change your User model

class User < ActiveRecord::Base
  ...

  has_one :finance
end

With the has_one association, you can use @finance = current_user.finance in your controller

Now, display it in your view using something like:

<%= @finance.age %>

Hope that helps?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM