I have problems on a simple design problem. I have many companies. Each company have one or more commercial activity. Activities types are: ecommerce, local unit, television, automatic vending machines, and so on. Company may have one or more ecommerce activity or one or more local unit activity, one ormore vending machines, etc. How can I model this scenario? Company has_many ecommerce has_many local_units has_many televisions and so on? -- 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 Thu, May 10, 2012 at 10:57 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have problems on a simple design problem. > I have many companies. Each company have one or more commercial activity. > Activities types are: ecommerce, local unit, television, automatic > vending machines, and so on. >So can each Company have many CommercialActivities Then.. CommercialActivity is STI, which means it has a type column and sub-types can be ECommerceActivity, TelevisionActivity, etc.. The only warning is that if each type is dramatically different, you might want to investigate doing it with multiple tables so you dont have a lot of redundant fields trying model things with additional attributes that aren''t shared. I think this post might help you http://stackoverflow.com/questions/1634668/multiple-table-inheritance-vs-single-table-inheritance-in-ruby-on-rails -- Greg Akins http://twitter.com/akinsgre -- 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 10 May 2012 16:05, Greg Akins <angrygreg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Thu, May 10, 2012 at 10:57 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I have problems on a simple design problem. >> I have many companies. Each company have one or more commercial activity. >> Activities types are: ecommerce, local unit, television, automatic >> vending machines, and so on. >> > > So can each Company have many CommercialActivities > > Then.. CommercialActivity is STI, which means it has a type column and > sub-types can be ECommerceActivity, TelevisionActivity, etc.. > > The only warning is that if each type is dramatically different, you might > want to investigate doing it with multiple tables so you dont have a lot of > redundant fields trying model things with additional attributes that aren''t > shared. > > I think this post might help you > http://stackoverflow.com/questions/1634668/multiple-table-inheritance-vs-single-table-inheritance-in-ruby-on-rails > >You can also use a project of mine (Super STI) to help you. It helps you use STI in Rails while keeping your database clean. Read about it at https://github.com/ihid/super_sti and http://www.ihid.co.uk/projects/super_sti Message me off-list if you want some help with using it.> -- > Greg Akins > http://twitter.com/akinsgre > > > > -- > 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. >-- 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 10 May 2012 17:05, Greg Akins <angrygreg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Thu, May 10, 2012 at 10:57 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> I have problems on a simple design problem. >> I have many companies. Each company have one or more commercial activity. >> Activities types are: ecommerce, local unit, television, automatic >> vending machines, and so on. > > > So can each Company have many CommercialActivities > > Then.. CommercialActivity is STI, which means it has a type column and > sub-types can be ECommerceActivity, TelevisionActivity, etc.. > > The only warning is that if each type is dramatically different, you might > want to investigate doing it with multiple tables so you dont have a lot of > redundant fields trying model things with additional attributes that aren''t > shared.Yes the problem is that television, ecommerce, etc. they don''t share the same attributes. -- 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 10 May 2012 16:26, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10 May 2012 17:05, Greg Akins <angrygreg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > On Thu, May 10, 2012 at 10:57 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > >> I have problems on a simple design problem. > >> I have many companies. Each company have one or more commercial > activity. > >> Activities types are: ecommerce, local unit, television, automatic > >> vending machines, and so on. > > > > > > So can each Company have many CommercialActivities > > > > Then.. CommercialActivity is STI, which means it has a type column and > > sub-types can be ECommerceActivity, TelevisionActivity, etc.. > > > > The only warning is that if each type is dramatically different, you > might > > want to investigate doing it with multiple tables so you dont have a lot > of > > redundant fields trying model things with additional attributes that > aren''t > > shared. > > Yes the problem is that television, ecommerce, etc. they don''t share > the same attributes. > >Use a polymorphic link table. So tables: companies: (name:string) company_activities: (company_id:int, activity_id:int, activity_type:string) television_activities(...) ecommerce_activitiies(...) Models: class Company < ActiveRecord::Base has_many :company_activities has_many :activities, through: company_activities end class CompanyActivity < ActiveRecord::Base belongs_to :company belongs_to :activity, polymorphic:true end and then lots of: class TelevisionActivity < ActiveRecord::Base has_many :company_activities has_many :companies, through: company_activities end class EcommerceActivity < ActiveRecord::Base has_many :company_activities has_many :companies, through: company_activities end etc Those could all inherit from Activity if you want to use STI, but they don''t need to.> -- > 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. > >-- 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.
Sorry for my ignorance, in your example have you forgot Activity model and activities table? On 10 May 2012 17:37, Jeremy Walker <jez.walker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 10 May 2012 16:26, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> On 10 May 2012 17:05, Greg Akins <angrygreg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > >> > >> > On Thu, May 10, 2012 at 10:57 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> >> I have problems on a simple design problem. >> >> I have many companies. Each company have one or more commercial >> >> activity. >> >> Activities types are: ecommerce, local unit, television, automatic >> >> vending machines, and so on. >> > >> > >> > So can each Company have many CommercialActivities >> > >> > Then.. CommercialActivity is STI, which means it has a type column and >> > sub-types can be ECommerceActivity, TelevisionActivity, etc.. >> > >> > The only warning is that if each type is dramatically different, you >> > might >> > want to investigate doing it with multiple tables so you dont have a lot >> > of >> > redundant fields trying model things with additional attributes that >> > aren''t >> > shared. >> >> Yes the problem is that television, ecommerce, etc. they don''t share >> the same attributes. >> > > Use a polymorphic link table. > > So tables: > companies: (name:string) > company_activities: (company_id:int, activity_id:int, activity_type:string) > television_activities(...) > ecommerce_activitiies(...) > > Models: > class Company < ActiveRecord::Base > has_many :company_activities > has_many :activities, through: company_activities > end > > class CompanyActivity < ActiveRecord::Base > belongs_to :company > belongs_to :activity, polymorphic:true > end > > and then lots of: > > class TelevisionActivity < ActiveRecord::Base > has_many :company_activities > has_many :companies, through: company_activities > end > > class EcommerceActivity < ActiveRecord::Base > has_many :company_activities > has_many :companies, through: company_activities > end > > etc > > Those could all inherit from Activity if you want to use STI, but they don''t > need to. > >> >> -- >> 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. >> > > -- > 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.-- 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.
On 11 May 2012 09:12, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry for my ignorance, in your example have you forgot Activity model > and activities table?ok, sorry they don''t need. -- 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.