1

i am working on a project, in that project for every model we have two properties common

  1. Created_ts
  2. Created_by

I want to add a base class in which i want to set these properties, i tried using

class BaseModel < ActiveRecords::Base

end

class Person < BaseModel

end

when i create an instance of Person model it gives exception, i am not sure why it is giving exception, may be because it is looking for table BaseModels.

can any one please suggest, how to do this.

Exception :

PG::UndefinedTable: ERROR:  relation "base_models" does not exist
LINE 5:                WHERE a.attrelid = '"base_models"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"base_models"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "base_models" does not exist
LINE 5:                WHERE a.attrelid = '"base_models"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"base_models"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
rajansoft1
  • 1,336
  • 2
  • 18
  • 38
  • can you give more details about the exception thrown please. – Lynch Mar 11 '14 at 08:47
  • 1
    you can encapsulate the common functionality inside a Module and let ActiceRecord::Base include that module which then becomes available for all of your models – Amol Pujari Mar 11 '14 at 08:48
  • @Lynch i have added the exception details – rajansoft1 Mar 11 '14 at 08:50
  • @AmolPujari can you please give me some example Url may be, it might help me understand it better – rajansoft1 Mar 11 '14 at 08:50
  • @AmolPujari is it a good idea to edit ActiveRecord::Base class – rajansoft1 Mar 11 '14 at 08:51
  • @RicardoDelaFuente Thanks for the help but i am not looking for single table inheritance, i wanted to use a base class in which i can place common functionalities – rajansoft1 Mar 11 '14 at 08:54
  • @rajansoft1 yes it is a good idea to open ActiveRecord::Base while ensuring you do not override any known/hidden features or at least most commonly used – Amol Pujari Mar 11 '14 at 09:39
  • 1
    check out this one - http://stackoverflow.com/questions/7490681/how-can-i-achieve-a-functionality-similar-to-auto-insertion-of-timestamps – Amol Pujari Mar 11 '14 at 09:42

1 Answers1

0

we just need to make the base class as abstract class

for more reference seek below link

Rails extending ActiveRecord::Base

Community
  • 1
  • 1
rajansoft1
  • 1,336
  • 2
  • 18
  • 38