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.


> 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?

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
r...@agileconsultingllc.com



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to