From the ActiveRecord docs:
"Active Record can serialize any object in text columns using YAML"
so the column type is just :text.
Chris
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/.
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.
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
--
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
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
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.
Exactly.
> Craig
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
--
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
Relational database design has nothing to do with MVC. Nothing at all.
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
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