Forum: Ruby on Rails Rails 3 Model Without Database

78c53c84c7af015c7ae9f5c43322c2cc?d=identicon&s=25 Jeff Miller (ibanez270dx)
on 2011-02-25 08:38
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
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2011-02-25 09:13
(Received via mailing list)
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.
08e5d88478b98b7c6d41d8ebf325e975?d=identicon&s=25 JB (Guest)
on 2011-02-25 10:16
(Received via mailing list)
Hi, Jeff!
Chap #2 of this Crafting Rails Applications (http://www.pragprog.com/
titles/jvrails/crafting-rails-applications) might help...

Regards,
jb
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2011-02-25 10:30
(Received via mailing list)
On 25 February 2011 08:12, Javix <s.cambour@gmail.com> 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
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2011-02-25 10:54
(Received via mailing list)
On 25 Feb 2011, at 10:27, Colin Law 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
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2011-02-25 11:16
(Received via mailing list)
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!
Bdf1f2ad97e7b9c9db1fa70e3cef48bf?d=identicon&s=25 Bryan Crossland (Guest)
on 2011-02-25 15:36
(Received via mailing list)
On Fri, Feb 25, 2011 at 1:38 AM, Jeff Miller <lists@ruby-forum.com>
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.
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2011-02-25 16:40
(Received via mailing list)
On 25 Feb 2011, at 15:30, Bryan Crossland 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
67b3c74ecba1b73c7516b33cadec6f83?d=identicon&s=25 Arailsdemo A. (arailsdemo_a)
on 2011-02-25 16:45
You should look into ActiveModel
http://railscasts.com/episodes/219-active-model

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
(https://github.com/plataformatec/mail_form/blob/ma...)
for another example.  (The mail_form gem is described in detail in the
'Crafting Rails Applications', previously mentioned by JB.)



Jeff Miller 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...
>
78c53c84c7af015c7ae9f5c43322c2cc?d=identicon&s=25 Jeff Miller (ibanez270dx)
on 2011-02-25 18:34
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.