4

How do I save multiple values in a single cell record in Ruby on Rails applications?

If I have a table named Exp with columns named: Education, Experience, and Skill, what is the best practice if I want users to store multiple values such as: education institutions or skills in a single row?

I'd like to have users use multiple text fields, but should go into same cell record.

For instance if user has multiple skills, those skills should be in one cell? Would this be best or would it be better if I created a new table for just skills?

Please advise,

Thanks

1
  • Which database are you targeting? Commented Sep 9, 2013 at 4:34

4 Answers 4

8

I would not recommend storing multiple values in the same database column. It would make querying very difficult. For example, if you wanted to look for all the users with a particular skill set, the query would clumsy both on readability and performance.

However, there are still certain cases where it makes sense.

  • When you want to allow for variable list of data points
  • You are not going to query the data based on one of the values in the list

ActiveRecord has built-in support for this. You can store Hash or Array in a database column.

  1. Just mark the columns as Text

    rails g model Exp experience:text education:text skill:text
    
  2. Next, serialize the columns in your Model code

    class Exp < ActiveRecord::Base
      serialize :experience, :education, :skill
      # other model code
    end
    
  3. Now, you can just save the Hash or Array in the database field!

    Exp.new(:skill => ['Cooking', 'Singing', 'Dancing'])
    
Sign up to request clarification or add additional context in comments.

5 Comments

Why is it that I get an argument error when I have serialize like that in my model?
there's a pretty long full trace error. is there something that I should be looking for? Or maybe just application trace which is this -> app/models/exp.rb:3:in '<class:HairExp>' app/models/exp.rb:1:in '<top (required)>' app/controllers/exp_controller.rb:1:in '<top (required)>'
sorry it should be Exp not HairExp
Maybe an easier way is if I create a new table for each subject? a table for skill, a table for experience, another table for education? Would this be better in the long run?
Yes, that is absolutely the best approach. It would make things much more easy to maintain, e.g. you can add new data points later on, you can query by these attributes, etc.
1

You can do it using a serialized list in a single column (comma-separated), but a really bad idea, read these answers for reasoning:

I suggest changing your schema to have a one to many relationship between users and skills.

Comments

0

Rails 4 and PostgreSQL comes with hstore support out of the box, more info here In rails 3 you can use gem to enable it.

1 Comment

@j03w Thank you! Yes arrays are also supported.
0

It depends on what kind of functionality you want. If you want to bind the Exp model attributes with a form (for new and update operations) and put some validations on them, it is always better to keep it in a separate table. On the other hand, if these are just attributes, which you just need in database keep them in a single column. There is way by which you can keep the serialized object like arrays and hashes in database columns. Make them a array/hash as per your need and save it like this.

http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize

Serialized attributes, automatically deserializes when they are pulled out of tables and serialized automatically when saved.

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.