0

I have three models using inheritance, and a table named 'telecom_circuits'.

class Telecom::Circuits::BaseCircuit < ActiveRecord::Base
end

class Telecom::Circuit < ::Telecom::Circuits::BaseCircuit
  ...
end

class Telecom::Circuits::AttVoiceCircuit < ::Telecom::Circuit
  self.table_name = 'telecom_circuits'
end

When I do a create on the inherited class, it cannot find the table.

[1] pry(main)> Telecom::Circuits::AttVoiceCircuit
=> Telecom::Circuits::AttVoiceCircuit(id: integer, user_id: integer, division_id: integer, raw_site_id: integer, install_date: date, saville_account_number: string, account_number: string, meg8_account_number: string, main_circuit_id: string, d_channel: string, d_channel_type: string, ds3_access_circuit_id: string, lec_circuit_id: string, cli: string, lso: string, apn_ct1: string, dchan_cost: decimal, monthly_recurring_cost: decimal, created_at: datetime, updated_at: datetime, circuit_provision_type: string, trunk_group: string, apn_ct2: string, slot: string, disconnect_date: datetime, disconnect_requester_id: integer, disconnect_processor_id: integer, telecom_site_id: integer)
[2] pry(main)> Telecom::Circuits::AttVoiceCircuit.create
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'itrc_dev.telecom_base_circuits' doesn't exist: SHOW FULL FIELDS FROM `telecom_base_circuits`
from /Users/mpierc200/.rvm/gems/ruby-1.9.3-p327@itrc/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
[3] pry(main)> Telecom::Circuits::AttVoiceCircuit.table_name
=> "telecom_circuits"

How do I get the inherited class to find the table? Keep in mind, I will need other inherited classes with different tables.

7
  • Same results with self.table_name = ... or set_table_name ... Sep 10, 2013 at 13:43
  • So your classes are using inheritance, but you have a table for each class (ie. you don't Single Table Inheritance)?
    – jokklan
    Sep 17, 2013 at 11:17
  • Correct. I'm using multiple table inheritance, with a table for each inherited class, but none for the base class. Sep 17, 2013 at 13:19
  • whats the result of Telecom::Circuit.table_name and Telecom::Circuits::BaseCircuit.table_name ?
    – Siva
    Sep 18, 2013 at 11:13
  • Telecom::Circuits::BaseCircuit.table_name => "telecom_base_circuits" Telecom::Circuit.table_name => "telecom_base_circuits" Sep 18, 2013 at 17:25

4 Answers 4

4
+100

This looks like a bug in Rails 3.2 that is fixed in Rails 4.0.0.

ETA: Actually, you might need to add abstract class to fix your immediate problem but then hit this bug ;)

Also see this related Documentation commit.

ETA again: A closed rails issue sounds related as well; having non-abstract-classes and table-per-class inheritance looks unsupported.

2
  • 1
    I also found as a work around that if I set self.superclass.table_name and ALSO set self.table_name it works. Sep 19, 2013 at 15:01
  • eugh, that sucks to have to do that though. Sep 19, 2013 at 18:00
1

You have a lot of concepts at play here. Ruby Inheritance, Rails STI, and Namespacing.

First, it seems like you're confusing Ruby's Object Oriented Inheritance with Rails Single Table inheritance (STI) because they both confusingly use inheritance. However with STI the key takeaway is the words single table. Rails STI makes the assumption that all objects will be saved into one table and that the model will add their class name into the extra db column called type. This column needs to exist for STI to work. It also allows you to fetch them via their class_name

Telecom::Circuits::Base.all

or

Telecom::Circuits::AttVoice.all

Your code as I understand it should be

# in app/telecom/circuit.rb
class Telecom::Circuit < ActiveRecord::Base
  ...
end

# in app/telecom/circuits/base.rb
class Telecom::Circuits::Base < Telecom::Circuit
  ...
end

# in app/telecom/circuits/att_voice.rb    
class Telecom::Circuits::AttVoice < Telecom::Circuit
  ... 
end

I took the liberty of making base and att_voice a subclass of circuits as I assumed that they are both independent types of circuits. If base has functionality that the other sub classes share and add on too then this class is redundant and the code would go into the Telecom::Circuit class.

If this isn't what you want, you're outside of the ActiveRecord model used in Rails. You may need to give each its own fully fledged table. Or perhaps you can utilize DataMapper or the new ROM to achieve your means.

Hope this helps!

1
  • I'm trying to use Multiple Table Inheritance. I realize that Rails is focused on STI, and supports it very well. However, I have done this in the past, and gotten it to work. I don't see why Rails should not support this, and it makes it there one step short. Everything works but create is broken. Sep 19, 2013 at 15:00
0

You shouldnt inherit more than 1 level with rails, it becomes complicated and doesn't work very well. I had a similar structure and went back to only a single inheritance.

Im not sure if this works, but did you try it also with:

class BaseCircuit < ActiveRecord::Base
end

class Circuit < BaseCircuit
  ...
end

class AttVoiceCircuit < Circuit
  ...
end

so without namespaces? Table name should be "base_circuits"

Edit: In the answers someone suggested the problem is with more than one level of inheritance or namespaces. I tried the following:

class BaseSo < ActiveRecord::Base
end

class AttVoice < BaseSo
  self.table_name = 'telecom_circuits'
end

class ComcastVoice < BaseSo
  self.table_name = 'telecom_pri_circuits'
end

with these results:

[1]pry(main)> AttVoice
=> AttVoice(id: integer, user_id: integer, division_id: integer, raw_site_id: integer, install_date: date, saville_account_number: string, account_number: string, meg8_account_number: string, main_circuit_id: string, d_channel: string, d_channel_type: string, ds3_access_circuit_id: string, lec_circuit_id: string, cli: string, lso: string, apn_ct1: string, dchan_cost: decimal, monthly_recurring_cost: decimal, created_at: datetime, updated_at: datetime, circuit_provision_type: string, trunk_group: string, apn_ct2: string, slot: string, disconnect_date: datetime, disconnect_requester_id: integer, disconnect_processor_id: integer, telecom_site_id: integer)
[2] pry(main)> AttVoice.table_name
=> "telecom_circuits"
[3] pry(main)> ComcastVoice
=> ComcastVoice(id: integer, created_at: datetime, updated_at: datetime, order_submitter: string, division_id: integer, lead_id: integer, pin: integer, raw_site_id: integer, site_poc: string, install_date: date, csg_billing_number: string, gl_code: string, pri_count: integer, pri_type: string, port_native: boolean, did_range: string, did_count: integer, comments: string, telecom_site_id: integer)
[4] pry(main)> ComcastVoice.table_name
=> "telecom_pri_circuits"
[5] pry(main)> AttVoice.count
(41.8ms)  SELECT COUNT(*) FROM `telecom_circuits` 
=> 2247
[6] pry(main)> AttVoice.create
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'itrc_dev.base_sos' doesn't exist: SHOW FULL FIELDS FROM `base_sos`
from /Users/mpierc200/.rvm/gems/ruby-1.9.3-p327@itrc/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
[7] pry(main)> ComcastVoice.count
(0.4ms)  SELECT COUNT(*) FROM `telecom_pri_circuits` 
=> 155
[8] pry(main)> ComcastVoice.create
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'itrc_dev.base_sos' doesn't exist: SHOW FULL FIELDS FROM `base_sos`
from /Users/mpierc200/.rvm/gems/ruby-1.9.3-p327@itrc/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
6
  • The two levels are temporary, while I refactor Telecom::Circuit into a base class and the inherited AttVoiceCircuit. I would expect a solid programming language and web environment to handle any number of levels of inheritance. Sep 10, 2013 at 21:16
  • Maybe you should try it without the namespacing Sep 10, 2013 at 21:18
  • Did you create the table called base_sos? Sep 10, 2013 at 21:57
  • Usually if you make inheritance, it means that you only use a single table Sep 10, 2013 at 21:58
  • And why you want to make inheritance if you then use multiple tables? maybe its better to make some modules and include them in the classes? Sep 10, 2013 at 22:05
0

Would modules and mixins handle this for you?

See here.

2
  • I am trying to move common attributes to a common Circuit class, and so what I am trying to do does match the example. Sep 19, 2013 at 15:05
  • It would work, and get the goal of being able to change all my inherited classes by changing the "base" class (or in this case module). I am trying to use inheritance for a couple reasons. One, I have a strong isa relationship, so I want to use the primary inheritance mechanism. Two, I find that putting has_one in a module couples that module with restrictions on what the module can be included in. I feel more comfortable when the coupling is covered with other inheritance. That is, one assumption of the module is that it's an AR class so it can call has_one. Sep 19, 2013 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.