Simple question about code in a model class

hi,
yesterday i ran into an interesting problem which is im sure a silly
mistake.

do models contain any logic?

for example in my class sale i have this function

define get_category(category)
if category == 0
Sale.find_by_sql(“Select * from Sales WHERE sale_id = ?”,
category)
end
if category != 0
Sale.find_by_sql(“Select * from Sales”)
end
end

however this was not working when displaying data in my view. i tried
all variations of statements but no luck. When i put the if logic into
my controller and just created the same if statement in there, i had no
problem getting the data.

so i was thinking that if there is logic in a model class, than that
would take away from the controllers powers. is this true?

Thanks,

You’ll hear more than one point of view on this issue, here’s mine:

Storing business logic that is specific to the data a model represents
is what models are for. Do you find that there are complex things you
do with your models, over and over in many places? Factor out that
code and drop it onto the model, adding it as a method. Then you can
just ask the model for the answer rather than slogging through it each
time.

I’d recommend taking a look at some actual source code to see how
models are used. Technoweenie’s Beast is a good example… in
http://svn.techno-weenie.net/projects/beast/trunk/app/models/ ,
compare the Topic and User models to the Monitorship model to see the
range of possibilities.

–f

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs