0

I am having a model investor_profile.rb

I am having a field where he/she can choose various options(acquiring a business, lending to a business,...) in checkbox. Form code below.

<%= 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 %>

How can i design a database for storing multiple values? If it is a single value i can store it in interested_in column in investor_profile table.

3
  • Could you add the 2 models you want to join? Commented Jun 28, 2019 at 12:27
  • @LolWalid Can i use a third model called investor_interests and add list of values to it? like i can access it via user.investor_profiles.first.interested_in will return a list of selected interests? Commented Jun 28, 2019 at 12:49
  • That's the idea , i m posting my answer. You need to add a join_table ! (my response is with User And Intereset table) Commented Jun 28, 2019 at 13:02

3 Answers 3

1

As I understand you want to save several interests on your each investor_profiles record.

You have to create third model investor_interests, where you will have all of the interest options. You also need connection table called for example investor_profile_interests, which will belong to both, investor interests and investor profiles. In the investor_profile_interests table you will have investor_profile_id and investor_interest_id.

So that:
InvestorProfile has many InvestorProfileInterest
InvestorProfileInterest belongs to InvestorProfile
InvestorProfileInterest belongs to InvestorInterest

In this case you will have many to many relationship with investor_interests and will be able to store several interests on each profile.

connection table will look like following:

**investor_profile_id**  **investor_interest_id**    
-----------------------------------------------------
        1                        1
        1                        2
        1                        3
Sign up to request clarification or add additional context in comments.

Comments

1

What you want is a join table between your User and Interest models

You want to have something like this

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!

Let's do it with rails

First create the join_table through a migration, create_join_table rails documentation

# db/create_join_table_user_interest.rb
class CreateJoinTableUserInterest < ActiveRecord::Migration[5.1]
  def change
    create_join_table :user, :interest do |t|
      t.index %i[user_ud interest_id]
      t.index %i[interest_id user_ud]
    end
  end
end

Then create the joining model InterestUser, each row of the joining model belongs to both table!

# models/user_interest.rb
class InterestUser < ApplicationRecord
  belongs_to :user
  belongs_to :interest
end

Update your models to tell it, it has interests (look at the rails through tutorial )

# model/user.rb
class User < ApplicationRecord
  has_many :user_interests
  has_many :interests, through: :user_interest
end

# model/interest.rb
class Interest < ApplicationRecord
  has_many :user_interests
  has_many :user, through: :user_interest
end

Comments

0

When submitting your form, params["investor_profile"]["interested_in"] will return an array of string from interests in your controller.

That is why the simplest solution using the rails way is to store this value in a text column interested_in in your investor_profiles table.

Just add the following code in your model to serialize the array.

class InvestorProfile < ActiveRecord::Base
  serialize :interested_in, Array
end

Then you can save easily the params without formatting.

I made the assumption interests is only a list of tag and that you don't have interests table.

The limit of the serialization is that you cannot use efficient SQL query to filter investor_profiles on their interests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.