igorkasyanchuk / rails_db

Rails Database Viewer and SQL Query Runner
https://www.railsjazz.com/
MIT License
1.46k stars 111 forks source link

Issue with STI enabled table/model #63

Closed shivabhusal closed 7 years ago

shivabhusal commented 8 years ago

When I clicked the users link to see data-records, nothing happended. When I pried at Browser console, I saw 500 error. image

I saw the error in server log.

Schema of User model

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  first_name             :string           default("")
#  last_name              :string           default("")
#  phone_number           :string           default("")
#  address                :string           default("")
#  city                   :string           default("")
#  state                  :string           default("")
#  zip                    :string           default("")
#  date_of_birth          :date
#  gender                 :integer
#  metadata               :json
#  type                   :string
#  deleted_at             :datetime
#  status                 :integer          default(0)

System attributes

Rails: 5.0
Rails db: 1.3.3
ActiveRecord: 5.0.0.1

class User < ApplicationRecord
class VehicleOwner < User
class GarageOwner < User

Data in Table users

image

Backtrace

Started GET "/rails/db/tables/users/data" for 127.0.0.1 at 2016-09-01 22:36:34 +0545
Processing by RailsDb::TablesController#data as JS
  Parameters: {"table_id"=>"users"}
  Rendering /home/john/.rvm/gems/ruby-2.3.1/gems/rails_db-1.3.3/app/views/rails_db/tables/data.js.erb
  Rendered /home/john/.rvm/gems/ruby-2.3.1/gems/rails_db-1.3.3/app/views/rails_db/shared/_exp_col.html.erb (0.6ms)
  Rendered /home/john/.rvm/gems/ruby-2.3.1/gems/rails_db-1.3.3/app/views/rails_db/tables/_condition_fields.html.erb (59.2ms)
  Rendered /home/john/.rvm/gems/ruby-2.3.1/gems/rails_db-1.3.3/app/views/rails_db/tables/_condition_fields.html.erb (63.4ms)
   Load (0.8ms)  SELECT  "users".* FROM "users" LIMIT $1 OFFSET $2  [["LIMIT", 25], ["OFFSET", 0]]
  Rendered /home/john/.rvm/gems/ruby-2.3.1/gems/rails_db-1.3.3/app/views/rails_db/tables/_data.html.erb (287.0ms)
  Rendered /home/john/.rvm/gems/ruby-2.3.1/gems/rails_db-1.3.3/app/views/rails_db/tables/data.js.erb (289.4ms)
Completed 500 Internal Server Error in 320ms (ActiveRecord: 4.9ms)

ActionView::Template::Error (Invalid single-table inheritance type: VehicleOwner is not a subclass of ):
    79:       </tr>
    80:     </thead>
    81:     <tbody>
    82:       <% @records.each do |record| %>
    83:         <tr class="row_<%= record[@table.primary_key] %>">
    84:           <%= render 'row', record: record %>
    85:         </tr>

activerecord (5.0.0.1) lib/active_record/inheritance.rb:189:in `find_sti_class'
activerecord (5.0.0.1) lib/active_record/inheritance.rb:163:in `discriminate_class_for_record'
activerecord (5.0.0.1) lib/active_record/persistence.rb:67:in `instantiate'
activerecord (5.0.0.1) lib/active_record/querying.rb:50:in `block (2 levels) in find_by_sql'
activerecord (5.0.0.1) lib/active_record/result.rb:52:in `block in each'
activerecord (5.0.0.1) lib/active_record/result.rb:52:in `each'
activerecord (5.0.0.1) lib/active_record/result.rb:52:in `each'
activerecord (5.0.0.1) lib/active_record/querying.rb:50:in `map'
activerecord (5.0.0.1) lib/active_record/querying.rb:50:in `block in find_by_sql'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activerecord (5.0.0.1) lib/active_record/querying.rb:49:in `find_by_sql'
activerecord (5.0.0.1) lib/active_record/relation.rb:699:in `exec_queries'
activerecord (5.0.0.1) lib/active_record/relation.rb:580:in `load'
activerecord (5.0.0.1) lib/active_record/relation.rb:260:in `records'
activerecord (5.0.0.1) lib/active_record/relation/delegation.rb:38:in `each'
rails_db (1.3.3) app/views/rails_db/tables/_data.html.erb:82:in `__home_john__rvm_gems_ruby_______gems_rails_db_______app_views_rails_db_tables__data_html_erb__1077299699231663384_57123760'
actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
activesupport (5.0.0.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:in `block in render'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'

activerecord (5.0.0.1) lib/active_record/inheritance.rb:189:in 'find_sti_class'

image

shivabhusal commented 8 years ago

I think this error is originating from

Some Additional info

rails_db-1.3.3/app/controllers/rails_db/tables_controller.rb

image

pry(#<#<Class:0x00000005c108b8>>)> @table
=> #<RailsDb::Table:0x00000005274e30
 @data=#<RailsDb::TableData:0x00000005274638 @table=#<RailsDb::Table:0x00000005274e30 ...>>,
 @model=
  #<Class:0x0000000526bc68>(id: integer, first_name: string, last_name: string, phone_number: string, address: string, city: string, state: string, zip: string, date_of_birth: date, gender: integer, metadata: json, type: string, ..... , updated_at: datetime),
 @name="users">

pry > @model.class.name
=> "Class"

# I think it should have been "User" or "VehicleOwner"
# That is why its throwing error "ActiveRecord::SubclassNotFound: Invalid single-table inheritance type: VehicleOwner is not a subclass of "
# did you notice, there is nothing after "of"

Model factory

    # rails_db-1.3.3/app/controllers/rails_db/tables_controller.rb:49
    def create_model(table_name, &block)
      klass = Class.new(ActiveRecord::Base) do
        def self.model_name
          ActiveModel::Name.new(self, nil, table_name)
        end
        self.table_name = table_name
      end
      klass.class_eval(&block) if block_given?
      klass
    end
igorkasyanchuk commented 7 years ago

try now

shivabhusal commented 7 years ago

thanks @igorkasyanchuk

shivabhusal commented 7 years ago

It still shows this error when I click on my users table link in sidebar. I have used STI: Customer and Admin are derived from User model.


ActionView::Template::Error (Invalid single-table inheritance type: Customer is not a subclass of ):
    79:       </tr>
    80:     </thead>
    81:     <tbody>
    82:       <% @records.each do |record| %>
    83:         <tr class="row_<%= record[@table.primary_key] %>">
    84:           <%= render 'row', record: record %>
    85:         </tr>
igorkasyanchuk commented 7 years ago

ok, will check again @Snick555 please let's check again tomorrow

igorkasyanchuk commented 7 years ago

@shivabhusal try now ... with latest version 1.4.1 of gem

shivabhusal commented 7 years ago

@igorkasyanchuk wow awesome!! the problem is gone now.