MVC architecture of ROR

Hi,
As the ROR follows MVC architecture, then Is it good practice to
write queries inside controller?
If yes then, For what purpose we are using model?

Thanks & Regards,
Tushar

As the ROR follows MVC architecture, then Is it good practice to
write queries inside controller?
If yes then, For what purpose we are using model?

I’ve always tried to put as much of the query logic into the model
itself (google for fat model skinny controller).

I find I use named scopes a lot now so that in my controllers I can do
things like:

@posts = Post.approved.recent

and keep things nice and readable and my model would look something
like:

named_scope :approved, :conditions => {:is_approved =>
true}, :order => ‘created_at DESC’
named_scope :recent, :order => ‘created_at DESC’, :limit => 5

The duplicated :order’s is because I might not necessarily chain the
scopes…

-philip

Philip H. wrote:

As the ROR follows MVC architecture, then Is it good practice to
write queries inside controller?
If yes then, For what purpose we are using model?

I’ve always tried to put as much of the query logic into the model
itself (google for fat model skinny controller).

I find I use named scopes a lot now so that in my controllers I can do
things like:

@posts = Post.approved.recent

and keep things nice and readable and my model would look something
like:

named_scope :approved, :conditions => {:is_approved =>
true}, :order => ‘created_at DESC’
named_scope :recent, :order => ‘created_at DESC’, :limit => 5

The duplicated :order’s is because I might not necessarily chain the
scopes…

-philip
HI,
I am agree with this that, our model will look like this:
named_scope :approved, :conditions => {:is_approved =>
true}, :order => ‘created_at DESC’

But, each time we are not able to find the records by using modelling at
all. Sometimes we have to write the complex query for 2 or 3 tables,
then such type of queries where should I write?
Thanks,
Tushar

scopes…
then such type of queries where should I write?
Good point. Well, if it’s the complexity is due to the nature of the
action of the controller (some advanced search form say) then putting
it in the controller makes sense as you’d never call it from anywhere
else.

I’m partly guessing here since I don’t know your app, but putting it
in the controller makes sense in that case.

But if you find yourself making that same query in a variety of places
it should find it’s way into a model somewhere… just for code reuse
at a minimum.

-philip

Hi,

Jamis B. nailed it in his post:
http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model.
Kind regards
Nicolai

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