blinking bear
2008-Dec-05 15:09 UTC
Does passing the params collection to a model break mvc?
I want to pass the params collection from the controller to the model to parse filtering and sorting conditions. Does having a method in the model that takes the params from the controller break MVC? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Bloom
2008-Dec-07 21:01 UTC
Re: Does passing the params collection to a model break mvc?
Not necessarily. The params object is just a hash. There is no reason
why you can''t or shouldnt pass a configuration hash to a method. I do
something similar quite often. One instance I find myself working with
is XML API''s that I must generate. Many options are available on the
query string. To handle this I use a pattern similar to the example
below:
#Controller
@models = Model.collection_for_api(params[:model])
render :xml => @models
#Model
def self.collection_for_api(opts = {})
# assemble a bunch of named scopes based on opts
end
On Dec 5, 9:09 am, "blinking bear"
<blinkingb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I want to pass the params collection from the controller to the model to
> parse filtering and sorting conditions. Does having a method in the model
> that takes the params from the controller break MVC?
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Leonardo
2008-Dec-07 23:22 UTC
Re: Does passing the params collection to a model break mvc?
You''re passing unnecessary knowledge to your model. It definitely doesn''t need to know the controller and/or the action it''s being called from. And this information is by default in the params hash of any given controller action. Thus, it''s considered a bad practice - bad design. Your objects should assume as little as possible about any other structure on your application. -- Leonardo Borges www.leonardoborges.com On Dec 5, 4:09 pm, "blinking bear" <blinkingb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want to pass the params collection from the controller to the model to > parse filtering and sorting conditions. Does having a method in the model > that takes the params from the controller break MVC?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Bloom
2008-Dec-08 05:10 UTC
Re: Does passing the params collection to a model break mvc?
If you notice I only pass a section of the params has designed specifically to build the appropriate query, not the entire hash. On Dec 7, 5:22 pm, Leonardo <leonardo.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You''re passing unnecessary knowledge to your model. > It definitely doesn''t need to know the controller and/or the action > it''s being called from. And this information is by default in the > params hash of any given controller action. > Thus, it''s considered a bad practice - bad design. Your objects should > assume as little as possible about any other structure on your > application. > > -- > Leonardo Borgeswww.leonardoborges.com > > On Dec 5, 4:09 pm, "blinking bear" <blinkingb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I want to pass the params collection from the controller to the model to > > parse filtering and sorting conditions. Does having a method in the model > > that takes the params from the controller break MVC?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---