Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord ignoring set_table_name on save

#5345

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

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

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Kane
    Kane

    i cant confirm this.

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

    August 11th, 2010 @ 05:00 PM

  2. José Valim
    José Valim
    • State changed from new to needs-more-info
    • Importance changed from to Low

    August 12th, 2010 @ 05:01 PM

  3. Nicholas Faiz
    Nicholas Faiz

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

    August 13th, 2010 @ 03:46 AM

  4. Kane
    Kane

    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.

    August 13th, 2010 @ 05:23 PM

  5. Nicholas Faiz
    Nicholas Faiz

    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.

    August 14th, 2010 @ 03:16 AM

  6. Neeraj Singh
    Neeraj Singh
    • State changed from needs-more-info to resolved

    August 14th, 2010 @ 04:59 AM