所以我想制作一个用户的档案模型。协会就像
一个用户有_一个个人资料
这样做的要求是:
现在我想为其制作一个模型,但问题是我无法做出逻辑。
我希望它采用单个实体来表示姓名、电子邮件、联系人、图片、标题、可用性等。但是当涉及到教育、工作经验、社交媒体链接时,它可以要求多个实体。 就像我可以首先进入学校的教育领域,然后进入大学/大学一样,通过工作经验来做到这一点。输入一家公司的工作经验字段,然后输入其他公司的工作经验字段。可以输入 LinkedIn、github 等社交媒体链接。
我尝试创建一个逻辑,其中迁移包含以下字段: 它使用序列化概念。
class CreateProfiles < ActiveRecord::Migration[6.0]
def change
create_table :profiles do |t|
t.references :user, null: false, foreign_key: true
t.string :name, null: false
t.string :email
t.text :contact_info
t.text :social_media_links, array: true, default: []
t.text :education, array: true, default: []
t.text :work_experience, array: true, default: []
t.string :profile_picture
t.string :headline
t.text :key_skills, array: true, default: []
t.boolean :availability_for_mentorship, default: false
t.timestamps
end
end
end
是否还有其他方法可以做到这一点,例如使用嵌套表单等。因为我是初学者,所以如果你指导我哪种方法更好,以及我如何为此制定更好的逻辑
如果不需要,我建议您不要将数据保存在数组中。我的建议是为教育、工作和社交联系创建单独的模型。将数据保存到数组中存在局限性。
关系方法将是更好的解决方案,因为它具有灵活性和可维护性。您可以不使用数组,而是在 Profile
、
Education
、WorkExperience
和 SocialMediaLink
之间建模关联。这将允许您为每个记录存储多个记录,例如多个学位或工作经历,并且是一种更具可扩展性的方法。
迁移
class CreateProfiles < ActiveRecord::Migration[6.0]
def change
create_table :profiles do |t|
t.references :user, null: false, foreign_key: true
t.string :name, null: false
t.string :email
t.text :contact_info
t.string :profile_picture
t.string :headline
t.boolean :availability_for_mentorship, default: false
t.timestamps
end
end
end
class CreateEducations < ActiveRecord::Migration[6.0]
def change
create_table :educations do |t|
t.references :profile, null: false, foreign_key: true
t.string :degree
t.string :institution
t.date :graduation_date
t.timestamps
end
end
end
class CreateWorkExperiences < ActiveRecord::Migration[6.0]
def change
create_table :work_experiences do |t|
t.references :profile, null: false, foreign_key: true
t.string :position
t.string :company
t.date :start_date
t.date :end_date
t.text :responsibilities
t.timestamps
end
end
end
class CreateSocialMediaLinks < ActiveRecord::Migration[6.0]
def change
create_table :social_media_links do |t|
t.references :profile, null: false, foreign_key: true
t.string :platform
t.string :url
t.timestamps
end
end
end
型号
class Profile < ApplicationRecord
belongs_to :user
has_many :educations, dependent: :destroy
has_many :work_experiences, dependent: :destroy
has_many :social_media_links, dependent: :destroy
accepts_nested_attributes_for :educations, allow_destroy: true
accepts_nested_attributes_for :work_experiences, allow_destroy: true
accepts_nested_attributes_for :social_media_links, allow_destroy: true
end
class Education < ApplicationRecord
belongs_to :profile
end
class WorkExperience < ApplicationRecord
belongs_to :profile
end
class SocialMediaLink < ApplicationRecord
belongs_to :profile
end
您可以使用嵌套表单在单个表单中处理多个关联记录(例如教育、工作经验)的创建。
在 profiles 控制器中,您将允许像这样的嵌套属性:
class ProfilesController < ApplicationController
def new
@profile = Profile.new
@profile.educations.build
@profile.work_experiences.build
@profile.social_media_links.build
end
def create
@profile = Profile.new(profile_params)
if @profile.save
redirect_to @profile, notice: 'Profile was successfully created.'
else
render :new
end
end
private
def profile_params
params.require(:profile).permit(
:name, :email, :contact_info, :profile_picture, :headline, :availability_for_mentorship,
educations_attributes: [:id, :degree, :institution, :graduation_date, :_destroy],
work_experiences_attributes: [:id, :position, :company, :start_date, :end_date, :responsibilities, :_destroy],
social_media_links_attributes: [:id, :platform, :url, :_destroy]
)
end
end
在您的 个人资料表单视图中,您可以使用 Rails 的
educations
帮助器为 work_experiences
、social_media_links
和 fields_for
创建嵌套字段:
<%= form_with model: @profile do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<!-- Education Fields -->
<h3>Education</h3>
<%= f.fields_for :educations do |edu_form| %>
<%= edu_form.label :degree %>
<%= edu_form.text_field :degree %>
<%= edu_form.label :institution %>
<%= edu_form.text_field :institution %>
<%= edu_form.label :graduation_date %>
<%= edu_form.date_field :graduation_date %>
<% end %>
<!-- Work Experience Fields -->
<h3>Work Experience</h3>
<%= f.fields_for :work_experiences do |work_form| %>
<%= work_form.label :position %>
<%= work_form.text_field :position %>
<%= work_form.label :company %>
<%= work_form.text_field :company %>
<%= work_form.label :start_date %>
<%= work_form.date_field :start_date %>
<%= work_form.label :end_date %>
<%= work_form.date_field :end_date %>
<%= work_form.label :responsibilities %>
<%= work_form.text_area :responsibilities %>
<% end %>
<!-- Social Media Links Fields -->
<h3>Social Media Links</h3>
<%= f.fields_for :social_media_links do |sm_form| %>
<%= sm_form.label :platform %>
<%= sm_form.text_field :platform %>
<%= sm_form.label :url %>
<%= sm_form.url_field :url %>
<% end %>
<%= f.submit "Create Profile" %>
<% end %>
Education
、WorkExperience
或SocialMediaLink
添加更多字段,而无需修改序列化数据。