The naming of STI tables

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

On Mar 7, 4:30pm, Fearless F. [email protected] 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”

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.html

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs