Questions tagged [model]

Part of the MVC pattern, the Model manages the behaviour and data of the application.

The MVC (Model, View, Controller) pattern separates objects into one of three categories — models for maintaining data, views for displaying all or a portion of the data, and controllers for handling events that affect the model or view(s).

Frameworks such as Laravel, Rails, Django and ASP.NET MVC apply this pattern in the web development domain.

18996 questions
129
votes
5 answers

Removing a model in rails (reverse of "rails g model Title...")

rails g model Rating user_id:integer message:string value:integer How can I completely remove this model? Thanks
Colbern
  • 1,291
  • 2
  • 9
  • 3
123
votes
8 answers

Using helpers in model: how do I include helper dependencies?

I'm writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I'm cleaning up the input in the model before saving to database, using the…
O. Frabjous-Dey
  • 1,273
  • 2
  • 8
  • 5
114
votes
3 answers

How to load json into my angular.js ng-model?

I have what I think is probably a very obvious question, but I couldn't find an answer anywhere. I am just trying to load some JSON data from my server into the client. Right now, I am using JQuery to load it with an AJAX call (code below).
MJR_III
  • 1,155
  • 2
  • 8
  • 6
113
votes
8 answers

Rspec doesn't see my model Class. uninitialized constant error

I'm writing tests on Rspec for my models in Ruby on Rails application. And I receive this error while starting 'rspec spec' command: /spec/models/client_spec.rb:4:in `': uninitialized constant Client (NameError) I use Rails 4.0.0…
Stan
  • 1,267
  • 2
  • 8
  • 11
112
votes
7 answers

What is the difference between an MVC Model object, a domain object and a DTO

What is the difference between a MVC Model object, a domain object and a DTO? My understanding is: MVC Model object: Models the data to be displayed by a corresponding view. It may not map directly to a domain object, i.e. may include data from one…
Timothy Mowlem
  • 1,121
  • 2
  • 8
  • 3
111
votes
9 answers

Eloquent ORM laravel 5 Get Array of ids

I'm using Eloquent ORM laravel 5.1, and I want to return an array of ids greater than 0, my model is called test. I have tried : $test=test::select('id')->where('id' ,'>' ,0)->get()->toarray(); It returns : Array ( [0] => Array ( [id] => 1 ) [1] =>…
paranoid
  • 6,799
  • 19
  • 49
  • 86
109
votes
8 answers

How to update() a single model instance retrieved by get() on Django ORM?

I have a function which currently calls Models.object.get(), which returns either 0 or 1 model objects: if it returns 0, I create a new model instance in the except DoesNotExist clause of the function. Otherwise, I would like to update the fields…
zhuyxn
  • 6,671
  • 9
  • 38
  • 44
106
votes
9 answers

Rails: Validating min and max length of a string but allowing it to be blank

I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am using the below validations in the model, but this doesn't allow the user…
bgadoci
  • 6,363
  • 17
  • 64
  • 91
104
votes
4 answers

Rails "validates_uniqueness_of" Case Sensitivity

Here is the model (I am using SQLLite3): class School < ActiveRecord::Base validates_uniqueness_of :name end For example, after I add "Yale", I cannot add "Yale" but can add "yale." How can I make the validation case insensitive? EDIT: Found it…
GeekJock
  • 11,066
  • 13
  • 43
  • 44
98
votes
8 answers

How to rename rails controller and model in a project

I started a Rails app and everything works fine. But now, I would like to rename a controller and the associated model: I wanted to change the Corps controller to Stores and the same (without final s) for the model. Looking on google, people…
Hassen
  • 6,966
  • 13
  • 45
  • 65
96
votes
6 answers

Check if an object exists

I need to check if Model.objects.filter(...) turned up anything, but do not need to insert anything. My code so far is: user_pass = log_in(request.POST) # form class if user_pass.is_valid(): cleaned_info = user_pass.cleaned_data user_object…
sinθ
  • 11,093
  • 25
  • 85
  • 121
95
votes
7 answers

Raise a validation error in a model's save method in Django

I'm not sure how to properly raise a validation error in a model's save method and send back a clear message to the user. Basically I want to know how each part of the "if" should end, the one where I want to raise the error and the one where it…
Bastian
  • 5,625
  • 10
  • 44
  • 68
95
votes
10 answers

Rails: Update model attribute without invoking callbacks

I have a User model that has a :credits attribute. I want a simple button that will add 5 to the user's credits, through a route called "add" so that /users/3/add would add 5 to the credits of user id = 3. def add @user =…
Sam Stern
  • 24,624
  • 13
  • 93
  • 124
95
votes
5 answers

ASP.NET MVC Model vs ViewModel

OK, I have been hearing discussion about "ViewModels" in regards to MS's ASP.NET MVC. Now, that is intended to be a specific kind of Model, correct? Not a specific kind of View. To my understanding, it's a kind of Model that has a specific purpose…
Qcom
  • 18,263
  • 29
  • 87
  • 113
94
votes
1 answer

Django's ManyToMany Relationship with Additional Fields

I want to store some additional information in that, automatically created, ManyToMany join-table. How would I do that in Django? In my case I have two tables: "Employees" and "Projects". What I want to store is how much each of the employees…
rrb_bbr
  • 2,966
  • 4
  • 24
  • 26