Skip to main content
added 517 characters in body
Source Link
gaetanm
  • 773
  • 1
  • 5
  • 8

I'm currently creating a web application with Ruby on Rails and I'm facing an conceptual problem about the MVC pattern.

If I need to check, for example, if the name of a data that I want to add in the database exists in a given an array, where do I need to do this? In the controller or the model?

I read everywhere that the model only has to care about the database stuff so I guess it's the controller job.

But with Rails the model seems to do more stuff like the data validation (validate, :presence...) so I'm wondering if it's finally the model which does that?

If it's the model job, it means that I need to pass a controller attribute to the model (the array comes from an API), is it weird?

As asked I give you more details:

My application gets a list of items from an API. I include this list in a form (like in a select tag) and then when the form is submitted I get id of the selected item to finally add it in the database with other information from the form.

Before adding the data to the database I just want to be sure that the id of the item is valid (that's why I need to check if it is present or not in the array - in case of the user modify manually the HTML from his browser...).

I'm currently creating a web application with Ruby on Rails and I'm facing an conceptual problem about the MVC pattern.

If I need to check, for example, if the name of a data that I want to add in the database exists in a given an array, where do I need to do this? In the controller or the model?

I read everywhere that the model only has to care about the database stuff so I guess it's the controller job.

But with Rails the model seems to do more stuff like the data validation (validate, :presence...) so I'm wondering if it's finally the model which does that?

If it's the model job, it means that I need to pass a controller attribute to the model (the array comes from an API), is it weird?

I'm currently creating a web application with Ruby on Rails and I'm facing an conceptual problem about the MVC pattern.

If I need to check, for example, if the name of a data that I want to add in the database exists in a given an array, where do I need to do this? In the controller or the model?

I read everywhere that the model only has to care about the database stuff so I guess it's the controller job.

But with Rails the model seems to do more stuff like the data validation (validate, :presence...) so I'm wondering if it's finally the model which does that?

If it's the model job, it means that I need to pass a controller attribute to the model (the array comes from an API), is it weird?

As asked I give you more details:

My application gets a list of items from an API. I include this list in a form (like in a select tag) and then when the form is submitted I get id of the selected item to finally add it in the database with other information from the form.

Before adding the data to the database I just want to be sure that the id of the item is valid (that's why I need to check if it is present or not in the array - in case of the user modify manually the HTML from his browser...).

Source Link
gaetanm
  • 773
  • 1
  • 5
  • 8

MVC - Checking data in the model or the controller?

I'm currently creating a web application with Ruby on Rails and I'm facing an conceptual problem about the MVC pattern.

If I need to check, for example, if the name of a data that I want to add in the database exists in a given an array, where do I need to do this? In the controller or the model?

I read everywhere that the model only has to care about the database stuff so I guess it's the controller job.

But with Rails the model seems to do more stuff like the data validation (validate, :presence...) so I'm wondering if it's finally the model which does that?

If it's the model job, it means that I need to pass a controller attribute to the model (the array comes from an API), is it weird?