Rails - 创建不同配置文件的多态关联

Rails - Polymorphic Association to Create Different Profiles

尝试做到这一点,以便在创建用户时,根据他们 select 是学生还是公司,rails 将创建该用户的学生资料或公司资料轮廓。

我曾尝试使用多态关联进行设置,但无法弄清楚如何根据视图中 selected 的内容在模型层生成配置文件。

型号

class User < ActiveRecord::Base

 has_secure_password 

  has_one :student_profile, dependent: :destroy
  has_one :corporate_profile, dependent: :destroy
  has_many :searches, dependent: :destroy


  #attr_accessor :profile_type - removed due to Rails 4, pushed strong params in controller

  before_create :create_profile

      def create_profile
        if profile_type == 1
          build_student_profile
        else
          build_corporate_profile
        end
      end
 end

学生和公司简介

class CorporateProfile < ActiveRecord::Base # or possibly inherit from ActiveRecord::Base if not using inheritance

  belongs_to :user

end

class StudentProfile < ActiveRecord::Base # or possibly inherit from ActiveRecord::Base if not using inheritance

  belongs_to :user

end

查看

这里我有两个单选按钮来决定注册表单上的用户类型

<%= bootstrap_form_for(@user) do |f| %>

            <div class="field">
              <%= f.form_group :gender, label: { text: "Gender" }, help: "Are you a corporate or a student?" do %>
              <p></p>
                  <%= f.radio_button :profileable, 1, label: "Student",   inline: true %>
                  <%= f.radio_button :profileable, 2, label: "Corporate", inline: true %>
                <% end %>
          </div>

用户控制器

class UsersController < ApplicationController


  def index
      @users = User.paginate(page: params[:page], :per_page => 5).includes(:profile)
  end

  def show
  if params[:id] 
      @user = User.find(params[:id])
      # .includes(:profile)
    else 
      @user = current_user
    end
    @searches = Search.where(user_id: @user).includes(:state, city: [:profile])
  end


  def new
    @user = User.new
    #@corporateprofile = Corporateprofile.new

  end

  def create
    @user = User.new(user_params)

    if @user.save
      session[:user_id] = @user.id
      redirect_to widgets_index_path
    else
      redirect to '/signup'
    end
  end

private
  def user_params
    params.require(:user).permit(:firstname, :lastname, :email, :password, :profile_type)
  end

end

并且控制器上没有传递代码(因为我一直坚持这一点)。任何更好的建议或解决此问题的方法将不胜感激!

干杯

首先,您想将个人资料 类 重命名为 StudentProfile 和 CorporateProfile。这将需要 运行 迁移来更改您的 table 名称。

这个问题的答案取决于您希望 StudentProfile 和 CorporateProfile 有多大不同。如果它们完全不同甚至大部分不同,请将它们分开 类。如果它们大部分相同(换句话说,它们共享许多相同的方法),您应该创建一个 Profile(或 UserProfile)模型并让 StudentProfile 和 CorporateProfile 继承自该模型。

至于实现,应该是这样的:

# user.rb
class User < ActiveRecord::Base
  has_one :student_profile
  has_one :corporate_profile

  attr_accessor :profileable #probably change this to profile_type. Using attr_accessible because we want to use it during creation, but no need to save it on the user model, although it may not be a bad idea to create a column for user model and save this value.

  before_create :create_profile

  def create_profile
    if profileable == 1
      build_student_profile
    else
      build_corporate_profile
    end
  end
end

# student_profile.rb
class StudentProfile < UserProfile # or possibly inherit from ActiveRecord::Base if not using inheritance
  belongs_to :user

  # other student profile stuff here
end

而且公司档案模型看起来与学生档案一样。

此外,此时您应该使用 Rails 4,特别是如果您正在学习但不了解控制器和参数,因为这在 rails 3 和 4 之间有很大不同. 学过时的东西没用吧?


编辑:我应该提一下,我不认为你理解 rails 多态性。当一个模型属于多个模型时它应该是多态的,不是当它有不同的子类.

例如,如果您的应用有一个 Like 模型和其他类似 Post 模型的东西,并且用户可以喜欢其他用户的个人资料或帖子,那么这可能是多态性的一个很好的候选者,因为 Like可能属于 StudentProfiles 或 CorporateProfiles 或 Posts.