【问题标题】:Why isn't my join working?为什么我的加入不起作用?
【发布时间】:2015-03-31 13:17:26
【问题描述】:

一个用户有一个个人资料。个人资料属于用户。该查询似乎在 postgres 中工作,但我无法让关联在视图中正常工作。以下是查询结果:

  User Load (0.4ms)  SELECT "users".* FROM "users"   ORDER BY "users"."id" ASC
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 5 ORDER BY "users"."id" ASC LIMIT 1
  Profile Load (0.3ms)  SELECT  "profiles".* FROM "profiles"  WHERE "profiles"."user_id" = $1 LIMIT 1  [["user_id", 5]]

这是用户和个人资料的两种模型:

用户:

class User < ActiveRecord::Base

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable

   has_one :profile, dependent: :destroy

   accepts_nested_attributes_for :profile, allow_destroy: true

end

简介:

class Profile < ActiveRecord::Base

   belongs_to :user, polymorphic: true

end

控制器...

用户:

class UsersController < ApplicationController

   def index
      @profiles = Profile.all
      @users = User.all.order(:id)
      User.joins(:profile)
   end

   private
      def set_user
         @user = User.find(params[:id])
      end

 end

简介:

class ProfilesController < ApplicationController
   before_action :set_profile, only: [:show, :edit, :update, :destroy]

   # GET /profiles
   # GET /profiles.json
   def index
      @profiles = Profile.all
   end

   # GET /profiles/1
   # GET /profiles/1.json
   def show
   end

   # GET /profiles/new
   def new
      @profile = Profile.new
   end

   # POST /profiles
   # POST /profiles.json
   def create
      @profile = Profile.new(profile_params)

      respond_to do |format|
         if @profile.save
            format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
            format.json { render :show, status: :created, location: @profile }
         else
            format.html { render :new }
            format.json { render json: @profile.errors, status: :unprocessable_entity }
         end
      end
   end

 private

   def set_profile
      @profile = Profile.find(params[:id])
   end

   def profile_params
      params.require(:profile).permit(:user_id)
   end

结束

终于在 /users/index 视图中:

<%= user.profile.user_id %>

但是当我尝试渲染视图时,我收到以下错误:

nil:NilClass 的未定义方法 `user_id'

感谢您的帮助。

【问题讨论】:

    标签: mysql ruby-on-rails postgresql ruby-on-rails-4


    【解决方案1】:

    如果您确实不需要关联是多态的,则应将其从Profile 模型中删除。

    class Profile < ActiveRecord::Base
    
       belongs_to :user
    end
    

    但如果您确实需要关联是多态的,那么您需要更改User 模型。

    class User < ActiveRecord::Base
    
       devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
    
       has_one :profile,
          dependent: :destroy,
          as: user
    
       accepts_nested_attributes_for :profile, allow_destroy: true
    
    end
    

    您还需要在 Profile 模型中添加 user_type

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2018-05-27
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 2019-12-23
      相关资源
      最近更新 更多