Forum: Ruby on Rails Use of Models - The Very Basics

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
80159acdf89580a20e8e835772b5bf82?d=identicon&s=25 Doug Jolley (ddjolley)
on 2009-06-07 20:29
(Received via mailing list)
Embarrassingly, I must admit that I have never understood models.  I
am hoping that with this post I can clear up a basic question that
will allow me to get a toe-hold into understanding models.  The basic
question is this:

Do we only use models with databases or do they have other uses?

Models descend from ActiveRecord::Base.  That certainly gives rise to
a notion that models are used exclusively with databases.  However, I
have read plenty of chatter encouraging me to move code out of the
controller and into the model.

At the moment, I happen to be dealing with a perfect example.  In a
user's profile I store an integer named 'flags'.  The integer is a
decimal representation of a set of binary flags that correspond to
various yes/no configuration selections that the user has made.  I
have written a very short method that will accept 2 arguments, a
user's flags integer and the weight of a specific flag.  The method
returns true or false depending on whether the specific flag is set in
the given flags integer. The method has absolutely nothing to do with
a database.  The question is:  Where do I put this method? From the
description that I have given I think that it is clear that this
method is back-room, heavy-duty, number-crunching stuff (to use terms
that I have seen in my reading).  So, does it go in the model?  If so,
how do I access it from other places?

Thanks very much for any input.

          ... doug
5cabbcab339feace80762194dc9302ed?d=identicon&s=25 Christoph Jasinski (Guest)
on 2009-06-07 20:59
(Received via mailing list)
I'm not a Rails pro, but I would like to make the following comments:
A model

   - represents your Data you deal with, like students/products... which
you
   save in a database!
   - should include operations/methods which exclusively deal with the
   object/data, like setter/getter moethds and/or computing specific
things
   like an access/activation code

The convention is, as far as I now, to keep the controller clean and
light.
But in certain scenarios it make sense to keep some functionality in the
controller. However in you case I would definitely put it into the
model.
"The method has absolutely nothing to do with a database", sure but it
has a
lot to do with the data/objects you store there. You call that special
method on a specific object as I understand you point. You can do
something
like
user.your_method or user.your_method(variavble1,variable2) where you
define
your_method in the model file in rails.


Cheers & good luck,

Chris
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-06-07 23:50
(Received via mailing list)
On Jun 7, 2009, at 2:28 PM, doug wrote:
> Embarrassingly, I must admit that I have never understood models.  I
> am hoping that with this post I can clear up a basic question that
> will allow me to get a toe-hold into understanding models.  The basic
> question is this:
>
> Do we only use models with databases or do they have other uses?

Models let you encapsulate business logic.


> Models descend from ActiveRecord::Base.  That certainly gives rise to
> a notion that models are used exclusively with databases.  However, I
> have read plenty of chatter encouraging me to move code out of the
> controller and into the model.

Not always.  For example, I have a project with a SearchResult model;
it doesn't correspond directly to a table, but it does leverage
several other models (7, I think) that do persist data in the database.


> method is back-room, heavy-duty, number-crunching stuff (to use terms
> that I have seen in my reading).  So, does it go in the model?  If so,
> how do I access it from other places?
>
> Thanks very much for any input.
>
>          ... doug

So you're modeling a set of boolean flags, huh? Do you know about the
Integer#[] method?

irb> 3.downto(0) {|i| puts "10[#{i}]==#{10[i]}"}
10[3]==1
10[2]==0
10[1]==1
10[0]==0
=> 3
irb> 10.to_s(2)
=> "1010"

The point is that this method has everything to do with the *data*. It
is probably true that the data is persisted in the database, but this
method would make sense even if you kept the data in an Array, yes?

Put the method into the User model. Since you didn't give enough
specifics, I'll base an example on somewhat of a tangent since you
mentioned "back-room".

class User
   def liberal?
     self.flags[0].nonzero?
   end
   def moderate?
     self.flags[1].nonzero?
   end
   def conservative?
     self.flags[2].nonzero?
   end
end

Instead of asking political_leaning(doug.flags, User::Moderate) you
can just say doug.moderate? and be done. You could define constants
for the bit/flag numbers, too.

If this doesn't help you see how to use a model, post some code and
you might get a different style of answer.

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-06-08 02:27
Doug Jolley wrote:
[...]
> Do we only use models with databases or do they have other uses?

They certainly have other uses. Models represent any domain object that
you need.  Sometimes these objects will correspond to DB records,
sometimes not.

>
> Models descend from ActiveRecord::Base.  That certainly gives rise to
> a notion that models are used exclusively with databases.
[...]

The Rails community often gives the impression that models have to
descend from ActiveRecord::Base, but that's only true *if you need
ActiveRecord features*.  True, most models in Rails apps tend to inherit
from AR.  However, if you're modeling something that doesn't interact
with the DB, there's generally no need to inherit from AR, but the class
is still a model because it represents a domain object.
In other words, whether a class is considered a model has to do with the
*conceptual* niche that it fills within the app, not with whether it
inherits from AR.

Does that help?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
80159acdf89580a20e8e835772b5bf82?d=identicon&s=25 Doug Jolley (ddjolley)
on 2009-06-08 18:47
(Received via mailing list)
> Does that help?

Yes, it does.  So, I think that you are saying that, in the right
circumstances, it's perfectly fine to create a model that inherits
directly from object.  That does help.

FWIW, I'm still digesting Rob's response.  I think that there is
likely a lot of help there as well.  I just need to sort it all out.

Thanks for the input.

         ... doug
Af2ce6689213fdb78913a9662b18da6b?d=identicon&s=25 Rick Lloyd (ricklloyd)
on 2009-06-09 02:45
(Received via mailing list)
Imagine a small vendor needing to process credit card information but
not wanting, in any cases, to store the data locally.  Ideal case for
ActiveRecord methods without a database persistance.
This topic is locked and can not be replied to.