Questions about models and controllers

Hey all, i'm new here so let me introduce myself! My online name is cmekss and currently i'm a sophomore Math/CS major. I have been working on a website for the club I am in and so far things have been going pretty well. The premise of the website is to have people create a user profile on the website and then sign up for a variety of hiking trips offered on the website through the club.

However, I am running into a bit of problem. Given the nature of the website I have to do some customized queries onto the database. Naturally, I put these queries into the models as methods. However, I am having trouble understanding how I should be able to call those methods. Should I call them straight from the views? Or should I create a method in a controller that is related to the model? If I have to create a method in a controller, how do I do that while keeping the controllers RESTful? I am very confused as to how to do this and the online documentation of Ruby on Rails didn't help me understand it much.

Thanks for reading!

1 thought on “Questions about models and controllers”

  1. You should look into the Presenter pattern. Seems like a good fit for this case.

    In short, the Presenter object will have all the query methods on it. The Presenter knows how to get data out of the Model object, so your View will talk to an instance of the Presenter rather than directly interfacing with any Model objects.

    While MVP is often discussed as an alternative to MVC, Presenters should be quite compatible with MVC. In this case, your Controller will still be responsible for handling RESTful operations, but all the data processing required by your Views will move into the Presenter.

    EDIT: Idiomatic Rails usage would indeed be to place any nontrivial methods on the Model, but this approach is considered harmful by many as your app gets larger.


Leave a Comment