This project is archived and is in readonly mode.

#5345 ✓resolved
Nicholas Faiz

ActiveRecord ignoring set_table_name on save

Reported by Nicholas Faiz | August 10th, 2010 @ 04:08 AM

I've found this on Rails 3.0.0.rc

AR seems to be ignoring the table name set on the model when saving:

Loading development environment (Rails 3.0.0.rc)
ruby-1.8.7-p299 > Inkling::User.table_name
 => "inkling_users" 
ruby-1.8.7-p299 > u=Inkling::User.new(:login => "foo", :email => "foo@foo.com", :password => "test", :confirmation_password => "test")
 => #<Inkling::User id: nil, login: "foo", email: "foo@foo.com", role_id: nil, display_name: nil, given_name: nil, family_name: nil, crypted_password: nil, salt: nil, last_seen: nil, ip: nil, created_at: nil, updated_at: nil> 
ruby-1.8.7-p299 > u.save
ActiveRecord::StatementInvalid: PGError: ERROR:  relation "users" does not exist
LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"users"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Comments and changes to this ticket

  • Kane

    Kane August 11th, 2010 @ 05:00 PM

    i cant confirm this.

    could you provide more details? maybe the code of your model?

  • José Valim

    José Valim August 12th, 2010 @ 05:01 PM

    • State changed from “new” to “needs-more-info”
    • Importance changed from “” to “Low”
  • Nicholas Faiz

    Nicholas Faiz August 13th, 2010 @ 03:46 AM

    I've created a demo app for the problem here: http://github.com/biv/r3-bug-in-table-name . Apologies, I should have put more information on the ticket originally - I was rushed at the time.

    Note, I'm running this using Postgresql 9.0 beta and using ruby 1.8.7.

    The problem occurs when using inheritance on models. In this case a Mob::User < ActiveRecordBase exists in lib/mob. The model foo.rb extends it and sets a table name. When saving a foo the validation sequence is triggered. The validation in Mob::User looks for a table name 'users', despite a Foo being created (with a table name of 'other_foos').

    Let me know if you need any further info..

  • Kane

    Kane August 13th, 2010 @ 05:23 PM

    Im not quite sure that this worked in any previous versions.
    normally AR assumes STI if you inherit from another model.
    if you dont want that you could declare the parent model as abstract.

     class MyModel < ActiveRecord::Base
       self.abstract_class = true
     end
    

    Rails cant do MTI.

  • Nicholas Faiz

    Nicholas Faiz August 14th, 2010 @ 03:16 AM

    Thanks. It did work without setting abstract_class in Rails 2.2.2 (have a working app which uses this lib there). But the abstract_class property fixes it. Should have thought to try that.

  • Neeraj Singh

    Neeraj Singh August 14th, 2010 @ 04:59 AM

    • State changed from “needs-more-info” to “resolved”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages