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.

17280 questions
557
votes
5 answers

How should a model be structured in MVC?

I am just getting a grasp on the MVC framework and I often wonder how much code should go in the model. I tend to have a data access class that has methods like this: public function CheckUsername($connection, $username) { try { …
Dietpixel
  • 9,313
  • 9
  • 26
  • 32
327
votes
8 answers

Ignore mapping one property with Automapper

I'm using Automapper and I have the following scenario: Class OrderModel has a property called 'ProductName' that isn't in the database. So when I try to do the mapping with: Mapper.CreateMap(); It generates an exception : "The…
Msam85
  • 3,514
  • 2
  • 16
  • 17
288
votes
14 answers

Doctrine2: Best way to handle many-to-many with extra columns in reference table

I'm wondering what's the best, the cleanest and the most simply way to work with many-to-many relations in Doctrine2. Let's assume that we've got an album like Master of Puppets by Metallica with several tracks. But please note the fact that one…
Crozin
  • 41,538
  • 12
  • 84
  • 134
215
votes
28 answers

Is there a way to get a collection of all the Models in your Rails app?

Is there a way that you can get a collection of all of the Models in your Rails app? Basically, can I do the likes of: - Models.each do |model| puts model.class.name end
mr_urf
  • 3,083
  • 3
  • 24
  • 29
205
votes
5 answers

Rails find_or_create_by more than one attribute?

There is a handy dynamic attribute in active-record called find_or_create_by: Model.find_or_create_by_(: => "") But what if I need to find_or_create by more than one attribute? Say I have a model to handle a M:M relationship…
tybro0103
  • 43,805
  • 32
  • 138
  • 167
202
votes
7 answers

AngularJS - Binding radio buttons to models with boolean values

I am having a problem binding radio buttons to an object whose properties have boolean values. I am trying to display exam questions retrieved from a $resource. HTML:
197
votes
4 answers

Rails :dependent => :destroy VS :dependent => :delete_all

In rails guides it's described like this: Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :delete_all Right, cool. But what's the difference between…
Sergey
  • 4,464
  • 6
  • 22
  • 30
170
votes
7 answers

Determine what attributes were changed in Rails after_save callback?

I'm setting up an after_save callback in my model observer to send a notification only if the model's published attribute was changed from false to true. Since methods such as changed? are only useful before the model is saved, the way I'm currently…
modulaaron
  • 2,476
  • 3
  • 20
  • 27
166
votes
7 answers

Accessing MVC's model property from Javascript

I have the following model which is wrapped in my view model public class FloorPlanSettingsModel { public int Id { get; set; } public int? MainFloorPlanId { get; set; } public string ImageDirectory { get; set; } public string…
Null Reference
  • 10,480
  • 37
  • 100
  • 168
143
votes
31 answers

Cannot overwrite model once compiled Mongoose

Not Sure what I'm doing wrong, here is my check.js var db = mongoose.createConnection('localhost', 'event-db'); db.on('error', console.error.bind(console, 'connection error:')); var a1= db.once('open',function(){ var user = mongoose.model('users',{…
Anathema.Imbued
  • 2,561
  • 4
  • 15
  • 17
137
votes
8 answers

ASP.NET MVC Yes/No Radio Buttons with Strongly Bound Model MVC

Does anyone know how to bind a Yes/No radio button to a boolean property of a Strongly Typed Model in ASP.NET MVC. Model public class MyClass { public bool Blah { get; set; } } View <%@ Page Title="blah" Inherits="MyClass"%>
brianstewey
  • 1,742
  • 3
  • 14
  • 14
119
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,191
  • 2
  • 8
  • 3
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
110
votes
5 answers

How to query database by id using SqlAlchemy?

I need to query a SQLAlchemy database by its id something similar to User.query.filter_by(username='peter') but for id. How do I do this? [Searching over Google and SO didn't help]
user507220
108
votes
6 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,123
  • 2
  • 7
  • 5
1
2 3
99 100