File this under mild pedantry, but it''s been bugging me. Assume I want to store different kinds of fruit in an STI table. My approach works, but I end up referring to "fruit_bases" throughout my code where I''d really prefer just "fruits". What I''ve got is something like: ============# File: db/schema.rb create_table "fruit_bases" do |t| t.string "type" t.id "basket_id" end # File: app/models/fruit.rb module Fruit def self.table_name_prefix ''fruit_'' end require ''fruit/base'' end # File: app/models/fruit/base.rb module Fruit class Base < ActiveRecord::Base belongs_to :basket # code common to all fruit end end # File: app/models/fruit/apple.rb module Fruit class Apple < Base # apple specific code end end # File: app/models/basket.rb class Basket < ActiveRecord::Base has_many :fruit_bases end ============ Instinct tells me that my table should be named simply "fruits", and that a Basket has_many :fruits, not :fruit_bases. What''s the rails-y way to accomplish that? - ff -- 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 Mar 7, 4:30 pm, Fearless Fool <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> File this under mild pedantry, but it''s been bugging me. Assume I want > to store different kinds of fruit in an STI table. My approach works, > but I end up referring to "fruit_bases" throughout my code where I''d > really prefer just "fruits". > > Instinct tells me that my table should be named simply "fruits", and > that a Basket has_many :fruits, not :fruit_bases. > > What''s the rails-y way to accomplish that?class Fruit < ActiveRecord::Base belongs_to :basket end class Apple < Fruit end class Basket < ActiveRecord::Base has_many :fruits end Ref: "Single Table Inheritance" http://api.rubyonrails.org/classes/ActiveRecord/Base.html -- 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.
Robb Kidd wrote in post #1050675:> class Fruit < ActiveRecord::Base > belongs_to :basket > end > > class Apple < Fruit > end > > class Basket < ActiveRecord::Base > has_many :fruits > end > > Ref: "Single Table Inheritance" > http://api.rubyonrails.org/classes/ActiveRecord/Base.htmlRobb: Pardon -- Although my code shows it, I should have stated explicitly that I am sticking all the STI sub-classes in a Fruit module and app/model/fruit subdirectory. I envision enough Fruit sub-classes that I don''t want them all in the toplevel app/model directory. -- 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 Thursday, March 8, 2012 9:00:53 AM UTC, Ruby-Forum.com User wrote:> > Robb: Pardon -- Although my code shows it, I should have stated > explicitly that I am sticking all the STI sub-classes in a Fruit module > and app/model/fruit subdirectory. I envision enough Fruit sub-classes > that I don''t want them all in the toplevel app/model directory. >I did notice this. You can still stick fruit.rb, apple.rb and your other fruit classes in app/models/fruit and prevent automatic namespacing which you are working around with your module. It involves explicitly adding app/models/fruit to Rails'' autoload paths. Find the following line in config/application.rb: config.autoload_paths += %W(#{config.root}/lib) ...and change it to... config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/models/fruit) I am not entirely certain this is the cleanest, most Railsy way, but it was the poison I picked over the complexity of unnecessary modules or namespaces. I can live with a path added to config. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/AAwomsz2ulEJ. 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.