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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 August 2010 03:35, madisonkel <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?From the ActiveRecord docs: "Active Record can serialize any object in text columns using YAML" so the column type is just :text. Chris -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris Mear wrote:> On 17 August 2010 03:35, madisonkel <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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? > > From the ActiveRecord docs: > > "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.> > ChrisBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> Chris Mear wrote: >> On 17 August 2010 03:35, madisonkel <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote:> Marnen Laibow-Koser wrote: >> Chris Mear wrote: >>> On 17 August 2010 03:35, madisonkel <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Tue, Aug 17, 2010 at 6:49 AM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Robert Walker wrote: > > Marnen Laibow-Koser wrote: > >> Chris Mear wrote: > >>> On 17 August 2010 03:35, madisonkel <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.> CraigBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 August 2010 22:00, Madison Kelly <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Tue, Aug 17, 2010 at 2:12 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 17 August 2010 22:00, Madison Kelly <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 August 2010 00:36, Madison Kelly <madisonkellly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote:> 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_treeYikes! 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.