Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing arrays in database using ActiveRecord

Tags:

I am on rails 2.3.8 & I am using mysql as db adapter. I want to store arrays in my database. After searching I could come up with this very useful article.

Now I need to use GUI for input & not only server console. So say I have a text field called nums which logically should have int array. What should be the format of nums so that it becomes easy to retrieve & store the array out of that string ?

like image 793
Nikhil Garg Avatar asked Jun 23 '10 09:06

Nikhil Garg


People also ask

Can you save an array in a database?

Although an array is one of the most common data types in the world of programming, MySQL actually doesn't support saving an array type directly. You can't create a table column of array type in MySQL. The easiest way store array type data in MySQL is to use the JSON data type.

Can you store array in SQL database?

Conclusion. As you can see, SQL Server does not include arrays. But we can use table variables, temporary tables or the STRING_SPLIT function. However, the STRING_SPLIT function is new and can be used only on SQL Server 2016 or later versions.

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.

Can you use ActiveRecord without rails?

ActiveRecord is commonly used with the Ruby-on-Rails framework but you can use it with Sinatra or without any web framework if desired.


2 Answers

If you use serialize then you shouldn't have to worry about how the data is stored within the text field, although it's actually YAML.

serialize is documented in the Rails/ActiveRecord API (scroll down to the section headed "Saving arrays, hashes, and other non-mappable objects in text columns")

For display, you need a format that is understandable to users and that can be easily converted back into an array in your code. Comma- or space-delimited?

Formatting for output:

delim = ',' # or ' ' for spaces, or whatever you choose array.join(delim) 

Converting back into an array might work as follows:

num_array = nums.split(delim).map(&:to_i) # or to_f if not integers 

or perhaps using String#scan?

num_array = nums.scan(/\d+/).map(&:to_i) # for positive integers 
like image 66
Mike Woodhouse Avatar answered Oct 20 '22 18:10

Mike Woodhouse


If you're using postgres and rails 4, now you have a better native option.

# db/migrate/20140207133952_create_books.rb create_table :books do |t|   t.string 'title'   t.string 'tags', array: true   t.integer 'ratings', array: true end add_index :books, :tags, using: 'gin' add_index :books, :ratings, using: 'gin'  # app/models/book.rb class Book < ActiveRecord::Base end  # Usage Book.create title: "Brave New World",             tags: ["fantasy", "fiction"],             ratings: [4, 5]  ## Books for a single tag Book.where("'fantasy' = ANY (tags)")  ## Books for multiple tags Book.where("tags @> ARRAY[?]::varchar[]", ["fantasy", "fiction"])  ## Books with 3 or more ratings Book.where("array_length(ratings, 1) >= 3") 

http://edgeguides.rubyonrails.org/active_record_postgresql.html

like image 25
Steven Barragán Avatar answered Oct 20 '22 20:10

Steven Barragán