Rails 3 Model Without Database

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…

Any ideas?

Thanks in advance,

  • Jeff

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.

Hi, Jeff!
Chap #2 of this Crafting Rails Applications (http://www.pragprog.com/
titles/jvrails/crafting-rails-applications) might help…

Regards,
jb

On 25 February 2011 08:12, Javix [email protected] wrote:

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.

I don’t believe that is true. I understood that as long as the model
does not derive from ActiveRecord then it should not look in the
database.

Colin

On 25 Feb 2011, at 10:27, Colin L. wrote:

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.

I don’t believe that is true. I understood that as long as the model
does not derive from ActiveRecord then it should not look in the
database.

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.

Best regards

Peter De Berdt

That’s actually not true. If you don’t specify them as active record
base subclasses they’ll be just regular ruby classes.

Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
Learn: http://sensei.zenunit.com/
New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy
process! Check it out now!

On 25 Feb 2011, at 15:30, Bryan C. wrote:

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.

Or you could just “include ActiveModel::Validations” in your Avatar
class and get both validations and error handling.

Best regards

Peter De Berdt

On Fri, Feb 25, 2011 at 1:38 AM, Jeff M. [email protected]
wrote:

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.

B.

You should look into ActiveModel

This will allow you to create model that has certain ActiveRecord like
capabilities like validations without the need for a database.

Look at the mail_form gem
(mail_form/lib/mail_form/shim.rb at main · heartcombo/mail_form · GitHub)
for another example. (The mail_form gem is described in detail in the
‘Crafting Rails Applications’, previously mentioned by JB.)

Jeff M. wrote in post #983799:

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…

Wow, thanks for the responses everybody!

I’ll be working on this a little later today, I’ll definitely try out
your suggestions.

Thanks!!

  • Jeff