I'm very new to Ruby and to Rails. I'm coming from PHP and CakePHP.
I would like to create a base model from which all other models will
inherit from.
I have done this:
class AppModel < ActiveRecord::Base
end
and
class Post < AppModel
end
I'm getting the following error:
Table 'blog_development.app_models' doesn't exist
My question is, how do I tell Rails not to look for a table named
"app_models" AND am I even taking the right approach here? Is there a
better way to do this?
I'm very new to Ruby and to Rails. I'm coming from PHP and CakePHP.
I would like to create a base model from which all other models will
inherit from.
I have done this:
class AppModel < ActiveRecord::Base
end
and
class Post < AppModel
end
I'm getting the following error:
Table 'blog_development.app_models' doesn't exist
My question is, how do I tell Rails not to look for a table named
"app_models" AND am I even taking the right approach here? Is there a
better way to do this?
Short answer: Use set_table_name
<ActiveRecord::Base; in Post. [Though I haven't tried this and there may be further complications.]
Long answer: You're almost certainly doing the wrong thing. If you want
to share code between non-similar models, don't do it using inheritance.
Create a module which encapsulates the functionality (people tend to put
such stuff in the lib/ directory), and include the module in the models
instead.
If it might be useful for several different projects, you could put your
module in a plugin, and then have the module automatically include
itself in ActiveRecord::Base (therefore making its behaviour available
to all models).
> My question is, how do I tell Rails not to look for a table named
> "app_models" AND am I even taking the right approach here? Is there a
> better way to do this?
class AppModel < ActiveRecord::Base
# define as abstract.
self.abstract_class = true
end
> I would like to create a base model from which all other models will inherit from.
with that approach this is exactly what your getting. however, you
don't tell much about your goals. so it's hard to tell, whether this
is the right thing to do.
Thanks for the help everyone. I really just wanted to have a base
model and have the option of overriding methods for all models or
adding functionality etc. I will try Fred and MaD's approach and see
how that goes. I also realized that my initial idea to define a
to_slug method in the AppModel is probably wrong and should probably
go within a module instead.
Thanks for the help everyone. I really just wanted to have a base
model and have the option of overriding methods for all models or
adding functionality etc. I will try Fred and MaD's approach and see
how that goes. I also realized that my initial idea to define a
to_slug method in the AppModel is probably wrong and should probably
go within a module instead.