[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Active Record Inheritence

Andrew Libby

10/14/2006 3:53:00 PM


Greetings,

I'm having some troubles getting single table inheritance
working. I'm outside of rails, and I _think_ I understand
the way this whole thing is supposed to work. I've got
tables in a PostgreSQL database, (principal and user, the
User class extends Principal).

When I run the following code I get an error like so:

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1789:in
`method_missing': undefined method `first_name=' for
#<User:0xb7d815cc @attributes={"type"=>"User"},
@new_record=true> (NoMethodError)

When I change the class definition of User to extend
ActiveRecord::Base the code works and I wind up with rows in
the users table.

Any ideas what I'm not doing correctly?


The code:

#!/usr/bin/ruby


#CREATE TABLE principals (
# id integer DEFAULT nextval('id_seq'::text) NOT NULL,
# "type" text
#);
#
#
#CREATE TABLE users (
# id integer DEFAULT nextval('id_seq'::text) NOT NULL,
# username text,
# "password" text,
# first_name text,
# last_name text
#);

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:username => "org",
:password => "xx",
:database => "org"
)

class Principal < ActiveRecord::Base

end

class User < Principal

end

u = User.new
u.first_name = 'joejoe'

--
Andrew Libby
Tangeis, LLC
Innovative IT Management Solutions
alibby@tangeis.com

3 Answers

dblack

10/14/2006 4:31:00 PM

0

Andrew Libby

10/14/2006 5:22:00 PM

0




dblack@wobblini.net wrote:
> Hi --
>
>
> If you want to do single table inheritance, you need to use a single
> table :-) You're using two (principals and users).
>
> Try the following:
>
> 1. add a type/text column to users
> 2. get rid of principals
> 3. in the models, do:
>
> class User < ActiveRecord::Base
> end
>
> class Principal < User
> end
>
> I know I've reversed the inheritance flow; it seems to make more sense
> this way, but you can of course switch it around.
>
>
> David
>


Thanks for your comments. It seems I completely missed
what single table inheritance is all about. You'd thing by
calling it single table, I'd have gotten that it was
_single_table_.. I'm hoping to have other principal types
in my system (groups, systems, users, etc). I guess I'll
need to work something out with
has_a, am I pointing in the right direction?

Thanks.

Andy


--
Andrew Libby
Tangeis, LLC
Innovative IT Management Solutions
alibby@tangeis.com

dblack

10/14/2006 5:41:00 PM

0