Ruby on rails Rails-使用复选框在数据库中存储多个值

Ruby on rails Rails-使用复选框在数据库中存储多个值,ruby-on-rails,ruby,database,postgresql,Ruby On Rails,Ruby,Database,Postgresql,我有一个模型投资者档案。rb 我有一个领域,他/她可以在复选框中选择各种选项(收购企业、贷款给企业等等)。表格代码如下 <%= form_with model: @investor_profile do |f| %> <%= f.label :interested_in %> <% interests.each do |activity| %> <%= f.check_box :interested_in, { m

我有一个模型投资者档案。rb

我有一个领域,他/她可以在复选框中选择各种选项(收购企业、贷款给企业等等)。表格代码如下

<%= form_with model: @investor_profile do |f| %>
      <%= f.label :interested_in %>
      <% interests.each do |activity| %>
        <%= f.check_box :interested_in, { multiple: true }, activity, nil %>
       <% end %>
      <%= f.submit %>
<%= end %>


如何设计用于存储多个值的数据库?如果它是单个值,我可以将其存储在投资者档案表的感兴趣列中。

据我所知,您希望在每个投资者档案记录中保存多个兴趣

您必须创建第三种模式的投资者权益,您将拥有所有的权益选项。 您还需要名为例如investor_profile_interests的连接表,它将同时属于investor interests和investor profiles。在investor_profile_interests表中,您将有investor_profile_id和investor_interests_id

So that:
InvestorProfile has many InvestorProfileInterest
InvestorProfileInterest belongs to InvestorProfile
InvestorProfileInterest belongs to InvestorInterest
在这种情况下,您将与投资者利益有多对多关系,并且能够在每个配置文件上存储多个利益

连接表如下所示:

**investor_profile_id**  **investor_interest_id**    
-----------------------------------------------------
        1                        1
        1                        2
        1                        3

据我所知,您希望在您的每个投资者档案记录中保存几项权益

您必须创建第三种模式的投资者权益,您将拥有所有的权益选项。 您还需要名为例如investor_profile_interests的连接表,它将同时属于investor interests和investor profiles。在investor_profile_interests表中,您将有investor_profile_id和investor_interests_id

So that:
InvestorProfile has many InvestorProfileInterest
InvestorProfileInterest belongs to InvestorProfile
InvestorProfileInterest belongs to InvestorInterest
在这种情况下,您将与投资者利益有多对多关系,并且能够在每个配置文件上存储多个利益

连接表如下所示:

**investor_profile_id**  **investor_interest_id**    
-----------------------------------------------------
        1                        1
        1                        2
        1                        3

您需要的是用户模型和兴趣模型之间的联接表

你想要这样的东西吗

users table
id
1
2
3

interests table 
id name
1 "interest 1"
2 "interest 2"
3 "interest 3"

interest_users table (the join table)
id interest_id user_id
1  1           1        # User 1 is interesed in interest 1
1  2           1        # User 1 is interesed in interest 2
1  3           1        # User 1 is interesed in interest 3
1  3           2        # User 2 is interesed in interest 3

User 3 is interesed in nothing!
让我们用rails来做吧

首先通过迁移创建join_表

更新你的模型,告诉它,它有兴趣(看图片)

#model/user.rb
类用户<应用程序记录
有很多:用户兴趣
有很多:兴趣,通过::用户兴趣
结束
#model/interest.rb
课程兴趣<申请记录
有很多:用户兴趣
有很多:用户,通过::用户感兴趣
结束

您需要的是用户模型和兴趣模型之间的联接表

你想要这样的东西吗

users table
id
1
2
3

interests table 
id name
1 "interest 1"
2 "interest 2"
3 "interest 3"

interest_users table (the join table)
id interest_id user_id
1  1           1        # User 1 is interesed in interest 1
1  2           1        # User 1 is interesed in interest 2
1  3           1        # User 1 is interesed in interest 3
1  3           2        # User 2 is interesed in interest 3

User 3 is interesed in nothing!
让我们用rails来做吧

首先通过迁移创建join_表

更新你的模型,告诉它,它有兴趣(看图片)

#model/user.rb
类用户<应用程序记录
有很多:用户兴趣
有很多:兴趣,通过::用户兴趣
结束
#model/interest.rb
课程兴趣<申请记录
有很多:用户兴趣
有很多:用户,通过::用户感兴趣
结束

提交表单时,
params[“投资者档案”][“感兴趣”]
将从控制器中的
interests
返回一个字符串数组

这就是为什么使用rails方式的最简单解决方案是将该值存储在您的
investor\u profiles
表中感兴趣的文本列中

只需将模型中的以下代码添加到数组中

class InvestorProfile < ActiveRecord::Base
  serialize :interested_in, Array
end
class InvestorProfile
然后,您可以轻松地保存参数而无需格式化

我假设
interests
只是一个标签列表,您没有
interests


序列化的限制是,您无法使用高效的SQL查询来筛选投资者的兴趣档案。

提交表单时,
params[“投资者的兴趣档案”][“兴趣档案”]
将从控制器中的
兴趣
返回一个字符串数组

这就是为什么使用rails方式的最简单解决方案是将该值存储在您的
investor\u profiles
表中感兴趣的文本列中

只需将模型中的以下代码添加到数组中

class InvestorProfile < ActiveRecord::Base
  serialize :interested_in, Array
end
class InvestorProfile
然后,您可以轻松地保存参数而无需格式化

我假设
interests
只是一个标签列表,您没有
interests


序列化的限制是,您无法使用高效的SQL查询来筛选投资者的兴趣档案。

您可以添加您想要加入的两个模型吗?@LolWalid我可以使用第三个称为投资者兴趣的模型并向其添加值列表吗?就像我可以通过user.investor\u profiles.first.interest\u来访问它一样,我将返回一个选定兴趣的列表?这就是我的想法,我正在发布我的答案。您需要添加一个join_表!(我的回答是关于用户和兴趣表)你能添加你想加入的两个模型吗?@LolWalid我能使用第三个名为investor_interests的模型并添加价值列表吗?就像我可以通过user.investor\u profiles.first.interest\u来访问它一样,我将返回一个选定兴趣的列表?这就是我的想法,我正在发布我的答案。您需要添加一个join_表!(我的回答是用户和兴趣表)