Storing a hash or array in the db

1 view
Skip to first unread message

madisonkel

unread,
Aug 16, 2010, 10:35:23 PM8/16/10
to Ruby on Rails: Talk
Hello --

I know that I can use serialize to declare hashes or arrays in a class
that need to be used in the db. From http://api.rubyonrails.org/classes/ActiveRecord/Base.html:

class User < ActiveRecord::Base
serialize :preferences
end

user = User.create(:preferences => { "background" => "black",
"display" => large })

My question is what type do you give the variable :preferences for the
migration? In:

ruby script/generate migration _add_hash_column preferences:???

What is ??? -- text, string, something else?

Thanks!
Madison

Chris Mear

unread,
Aug 17, 2010, 5:06:18 AM8/17/10
to rubyonra...@googlegroups.com

From the ActiveRecord docs:

"Active Record can serialize any object in text columns using YAML"

so the column type is just :text.

Chris

Marnen Laibow-Koser

unread,
Aug 17, 2010, 8:15:31 AM8/17/10
to rubyonra...@googlegroups.com

Right. Or :string...any field that can store text will work.

But I'd worry more about why you're storing an array in the DB. There
are some legitimate use cases for it, but usually it's a sign that you
need to do some more normalization of your DB schema.

>
> Chris

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Sent from my iPhone
--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
Aug 17, 2010, 9:39:14 AM8/17/10
to rubyonra...@googlegroups.com
Marnen Laibow-Koser wrote:
> Chris Mear wrote:
>> On 17 August 2010 03:35, madisonkel <madiso...@gmail.com> wrote:
>>> My question is what type do you give the variable :preferences for the
>>> migration? In:
> But I'd worry more about why you're storing an array in the DB. There
> are some legitimate use cases for it, but usually it's a sign that you
> need to do some more normalization of your DB schema.

Sounds to me like the OP is looking for a "cheap" way to store a bit of
schema-less user preferences data in the database. This would be like
using a key/value store, but without the overhead of setting up and
using something like Redis.

IMHO this is a fine use case for serializing a hash or array into a
database column. As long as the limitations and performance
considerations are well understood.

Marnen Laibow-Koser

unread,
Aug 17, 2010, 9:49:03 AM8/17/10
to rubyonra...@googlegroups.com
Robert Walker wrote:
> Marnen Laibow-Koser wrote:
>> Chris Mear wrote:
>>> On 17 August 2010 03:35, madisonkel <madiso...@gmail.com> wrote:
>>>> My question is what type do you give the variable :preferences for the
>>>> migration? In:
>> But I'd worry more about why you're storing an array in the DB. There
>> are some legitimate use cases for it, but usually it's a sign that you
>> need to do some more normalization of your DB schema.
>
> Sounds to me like the OP is looking for a "cheap" way to store a bit of
> schema-less user preferences data in the database. This would be like
> using a key/value store, but without the overhead of setting up and
> using something like Redis.

Looking at the original post again, I think I agree.

>
> IMHO this is a fine use case for serializing a hash or array into a
> database column.

Yes -- as long as it's really a question of flexible attributes, and not
just of being too lazy to design a proper schema. :)

> As long as the limitations and performance
> considerations are well understood.

Exactly.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Madison Kelly

unread,
Aug 17, 2010, 5:00:14 PM8/17/10
to rubyonra...@googlegroups.com
I'm using an array to store a list of catagories that a product will belong to. Since the number of catagories a product can belong to isn't set (could be 1 or could be 10), I was going to use an array. Does that sound like an appropriate use?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Craig White

unread,
Aug 17, 2010, 5:07:43 PM8/17/10
to rubyonra...@googlegroups.com
On Tue, 2010-08-17 at 14:00 -0700, Madison Kelly wrote:
> I'm using an array to store a list of catagories that a product will
> belong to. Since the number of catagories a product can belong to
> isn't set (could be 1 or could be 10), I was going to use an array.
> Does that sound like an appropriate use?
----
sounds like a classic 'many to many' relationship and you should have a
categories table and a product table and a categories_products join
table.

Craig


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Marnen Laibow-Koser

unread,
Aug 17, 2010, 5:08:14 PM8/17/10
to rubyonra...@googlegroups.com
Madison Kelly wrote:
> I'm using an array to store a list of catagories that a product will
> belong
> to. Since the number of catagories a product can belong to isn't set
> (could
> be 1 or could be 10), I was going to use an array. Does that sound like
> an
> appropriate use?

Absolutely not. For this, you should be using has_many and belongs_to
properly. You might find it helpful to read up on relational database
normalization (Wikipedia has some great articles).

This is what I meant about arrays being smelly in the DB. Usually (as
here) you want associated records in another table.

Marnen Laibow-Koser

unread,
Aug 17, 2010, 5:09:28 PM8/17/10
to rubyonra...@googlegroups.com
Craig White wrote:
> On Tue, 2010-08-17 at 14:00 -0700, Madison Kelly wrote:
>> I'm using an array to store a list of catagories that a product will
>> belong to. Since the number of catagories a product can belong to
>> isn't set (could be 1 or could be 10), I was going to use an array.
>> Does that sound like an appropriate use?
> ----
> sounds like a classic 'many to many' relationship and you should have a
> categories table and a product table and a categories_products join
> table.
>

Exactly.

> Craig

Colin Law

unread,
Aug 17, 2010, 5:12:59 PM8/17/10
to rubyonra...@googlegroups.com
On 17 August 2010 22:00, Madison Kelly <madiso...@gmail.com> wrote:
> I'm using an array to store a list of catagories that a product will belong
> to. Since the number of catagories a product can belong to isn't set (could
> be 1 or could be 10), I was going to use an array. Does that sound like an
> appropriate use?

No, I don't believe so. Provide a Categories model and have product
has_and_belongs_to_many categories (and vice versa). Or provide a
categories_products join table and use has_many through the join
table. See the Rails Guide on ActiveRecord Associations for more
details.

Colin

Madison Kelly

unread,
Aug 19, 2010, 7:36:52 PM8/19/10
to rubyonra...@googlegroups.com
Thank you for your help! I'm still learning how to use MVC, and I'm still having difficulty with some of the concepts. 
 
Can I ask a follow-up quesiton on how to set this up? I will have a product that can belong to one or many sub-catagories which in turn belong to one or many catagories. The same subcatagory name can belong to two different catagories (although its referring to a different group of products). For example: Fishingline (catagory) -> Long (subcatagory) and Knives (catagory) -> Long (subcatagory), where the products in both are different.
 
Additional requirements:
- in the store, display just products in a subcatagory (under a catagory), or display all products in the catagory (products belonging to all subcatagories).
- in the admin portal, be able to set the catagories and subcatagories in the product view
 
 So there needs to be a class to represent the catagories. Should there be a class for each subcatagory that has the catagory as a property?
 
Can the products view update the catagory that the product belongs to?
 
For displaying in the store, I currently have a store controller + views that display all of hte items. Can the store controller show the different views based on the catagory? Or should the store controller own the catagory buttons that direct to the catagory views that show the specific subset of products? Do I need view and controller for the catagory class?
 
Thanks in advance for any advice you can provide!
 
Madison


 

--

Colin Law

unread,
Aug 20, 2010, 4:44:42 AM8/20/10
to rubyonra...@googlegroups.com
On 20 August 2010 00:36, Madison Kelly <madiso...@gmail.com> wrote:
> Thank you for your help! I'm still learning how to use MVC, and I'm still
> having difficulty with some of the concepts.

Please don't top post, it makes it difficult to follow the thread.
For example who are you thanking here and what was the help. You have
to look down to the bottom of the post to find out.

>
> Can I ask a follow-up quesiton on how to set this up? I will have a product
> that can belong to one or many sub-catagories which in turn belong to one or
> many catagories. The same subcatagory name can belong to two different
> catagories (although its referring to a different group of products). For
> example: Fishingline (catagory) -> Long (subcatagory) and Knives (catagory)
> -> Long (subcatagory), where the products in both are different.

Sorry, I can't follow that. You say you have
A product can belong to one or many sub-categories (I think using the
word belong generically here rather than as in belongs_to).
A sub-category can belong to one or many categories.
Then you start talking about a sub-category name - can there be more
than one sub-category with the same name?
It might help if you specify the attributes of category and
sub-category, they have a name and the relationships to other objects,
do they have other attributes?
I do not understand the comment about groups of products at all. I
also don't understand the example, Sorry.

Colin

Marnen Laibow-Koser

unread,
Aug 20, 2010, 10:36:44 AM8/20/10
to rubyonra...@googlegroups.com
Madison Kelly wrote:
> Thank you for your help! I'm still learning how to use MVC, and I'm
> still
> having difficulty with some of the concepts.

Relational database design has nothing to do with MVC. Nothing at all.

Robert Walker

unread,
Aug 21, 2010, 2:07:18 AM8/21/10
to rubyonra...@googlegroups.com
Madison Kelly wrote:
> Can I ask a follow-up quesiton on how to set this up? I will have a
> product
> that can belong to one or many sub-catagories which in turn belong to
> one or
> many catagories. The same subcatagory name can belong to two different
> catagories (although its referring to a different group of products).
> For
> example: Fishingline (catagory) -> Long (subcatagory) and Knives
> (catagory)
> -> Long (subcatagory), where the products in both are different.

If I follow what you're describing correctly there is a relational
database pattern for implementing Categories as you describe. The
pattern depends on a type of tree structure using a reflexive
relationship.

Here a quick overview of the pattern and a link to a plugin that
implements it:

categories (Table)
+----+-----------+-----------------+
| id | parent_id | name |
+----+-----------+-----------------+
| 1 | null | Fishing Line |
| 2 | 1 | Long |
| 3 | null | Knives |
| 4 | 3 | Long |
+----|-----------|-----------------+

class Category < ActiveRecord::Base
acts_as_tree :order => 'name'
end

http://github.com/rails/acts_as_tree

Marnen Laibow-Koser

unread,
Aug 25, 2010, 12:44:38 PM8/25/10
to rubyonra...@googlegroups.com

Yikes! No. Adjacency lists such as acts_as_tree uses are a beautiful
naïve design for tree structures -- and should be avoided at all costs.
The problem is that (unless you're using Oracle, which has some
proprietary extensions to its SQL syntax), you need a separate SQL query
to get each level of descendants.

What you should be using for this is probably a *nested-set* structure,
which is made easy in Rails by the awesome_nested_set plugin. See Joe
Celko's articles on nested sets --
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
should get you started.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Reply all
Reply to author
Forward
0 new messages