"Active Record can serialize any object in text columns using YAML"
so the column type is just :text.
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.
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.
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.
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?
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?
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.
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.
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.
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?
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.
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:
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
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.