Use of Models - The Very Basics

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

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

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.

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

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

3.downto(0) {|i| puts "10[#{i}]==#{10[i]}"}

10[3]==1 10[2]==0 10[1]==1 10[0]==0 => 3

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

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

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.