Hello everyone,
I'm working on a Rails 3 social networking app (following RailsSpace
by Addison Wesley) but the book is old and using Rails 2. Thus far I've
been chugging along converting it along the way, but I'm running into
some trouble with my Avatar model. I'm trying to implement it without
having a table in the database. In accordance to the book, I created the
model manually (instead of with generate) and add an initialize
function. I created the corresponding controller and views, but when I
try to upload my avatar, it errors out telling me that database.avatars
doesn't exist.
I'm pretty sure I've got the controller and model right, but I just need
to tell Rails to NOT look in the database for this model...
All the classes inside 'models' folder are considered as ActiveRecord
classes and Rails will try to find a corresponding table in the
database named as plural of the model. It's a convention; To bypass it
and have a model independent from ActiveRecord class, create it in
'lib' folder and require it in the class where you need it.
Indeed it’s not true, you can have any class in the models directory. It’s up to you as a developer to decide whether it belongs there.
In our applications, it’s quite common we have modellike behavior in plain Ruby classes. Heck, with Rails 3, being able to mix in ActiveModel modules in a non-AR class should make it very obvious that a model isn’t necessarily a one-on-one with a database table.
I’m working on a Rails 3 social networking app (following RailsSpace
by Addison Wesley) but the book is old and using Rails 2. Thus far I’ve
been chugging along converting it along the way, but I’m running into
some trouble with my Avatar model. I’m trying to implement it without
having a table in the database. In accordance to the book, I created the
model manually (instead of with generate) and add an initialize
function. I created the corresponding controller and views, but when I
try to upload my avatar, it errors out telling me that database.avatars
doesn’t exist.
This is because chapter 12 of that book tells you to create your Avatar model like so:
class Avatar < ActiveRecord::Base
When you inherit from ActiveRecord::Base it checks to see that you have a table in the database that matches Avatar. Back when the book was written I’m sure the rules of having a table were not as strict so the author inherited from ActiveRecord::Base as a short cut for not writing his own error handling (see page 378 section 12.2.3).
I’m pretty sure I’ve got the controller and model right, but I just need
to tell Rails to NOT look in the database for this model…
Any ideas?
Yes. As others have suggested correctly you do not need to inherit from ActiveRecord::Base in order to have a class in the models folder. Remove < ActiveRecord::Base from the top of your Avatar class. That will take care of it not looking into the database anymore for an Avatar table. After that you will need to write your own error handling for the areas where you were using AR’s build in error handling. You’ll see them pretty quick when you run your code. The will most likely show up as Undefined Method errors for “errors.add_to_base” and so on.