Questions tagged [model]
Part of the MVC pattern, the Model manages the behaviour and data of the application.
model
21,468
questions
578
votes
5
answers
202k
views
How should a model be structured in MVC? [closed]
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($...
430
votes
16
answers
307k
views
Get Android Phone Model programmatically , How to get Device name and model programmatically in android?
I would like to know if there is a way for reading the Phone Model programmatically in Android.
I would like to get a string like HTC Dream, Milestone, Sapphire or whatever...
385
votes
10
answers
363k
views
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<...
294
votes
13
answers
114k
views
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 ...
240
votes
29
answers
134k
views
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
213
votes
4
answers
113k
views
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 => :...
210
votes
5
answers
119k
views
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_<attribute>(:<attribute> => "")
But what if I need to find_or_create by more than ...
209
votes
51
answers
235k
views
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('...
207
votes
7
answers
157k
views
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 ...
206
votes
7
answers
229k
views
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:
<label data-ng-repeat="...
195
votes
11
answers
434k
views
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 ...
148
votes
9
answers
48k
views
Difference between interfaces and classes in Typescript
What is the different between Typescript Interfaces and Classes? When do I use
a Class? When do I use Interfaces? What are the advantages of them?
I need to create some kind of types for an http-...
141
votes
8
answers
153k
views
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
<%@ ...
134
votes
6
answers
169k
views
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]
133
votes
6
answers
111k
views
What is difference between a Model and an Entity [closed]
I am seeking clarification on the definitions of the terms Entity, Model, DataModel, and ViewModel within the context of software development. Could someone provide a detailed explanation for each of ...
129
votes
5
answers
144k
views
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
126
votes
8
answers
117k
views
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 ...
115
votes
8
answers
93k
views
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 `<top (required)>': ...
115
votes
7
answers
72k
views
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 ...
114
votes
3
answers
257k
views
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 ...
113
votes
9
answers
251k
views
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()->...
111
votes
8
answers
238k
views
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 ...
110
votes
9
answers
111k
views
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 ...
110
votes
4
answers
80k
views
Create if doesn't exist
I have a Django application that reads data from a web API and puts it in a database.
Is there a way to create a new object from a mode but prevent the duplicate exception if the object already exists?...
106
votes
4
answers
68k
views
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 ...
98
votes
8
answers
73k
views
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) ...
97
votes
6
answers
187k
views
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():
...
96
votes
5
answers
87k
views
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,...
96
votes
1
answer
56k
views
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 ...
96
votes
7
answers
102k
views
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 ...
95
votes
10
answers
94k
views
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 ...
94
votes
10
answers
68k
views
Using ActiveRecord, is there a way to get the old values of a record during after_update
Setup using a simple example: I've got 1 table (Totals) that holds the sum of the amount column of each record in a second table (Things).
When a thing.amount gets updated, I'd like to simply add the ...
94
votes
3
answers
19k
views
models.py getting huge, what is the best way to break it up?
Directions from my supervisor:
"I want to avoid putting any logic in the models.py. From here on out, let's use that as only classes for accessing the database, and keep all logic in external classes ...
94
votes
5
answers
39k
views
DisplayNameFor() From List<Object> in Model
I believe this is pretty simple, I just can't seem to find the right way to show the display name for an item within a list within my model.
My simplified model:
public class PersonViewModel
{
...
92
votes
5
answers
86k
views
Django Queryset with filtering on reverse foreign key
I have the following Django model:
class Make:
name = models.CharField(max_length=200)
class MakeContent:
make = models.ForeignKey(Make)
published = models.BooleanField()
I'd like to know ...
89
votes
2
answers
65k
views
Reusing a Model Built in R
When building a model in R, how do you save the model specifications such that you can reuse it on new data? Let's say I build a logistic regression on historical data but won't have new observations ...
85
votes
8
answers
54k
views
Django get list of models in application
So, i have a file models.py in MyApp folder:
from django.db import models
class Model_One(models.Model):
...
class Model_Two(models.Model):
...
...
It can be about 10-15 classes.
How to find ...
85
votes
4
answers
72k
views
Differentiating between domain, model, and entity with respect to MVC
Can someone explain these 3 concepts and the differences between them with respect to an MVC framework along with an example. To me these appear almost equivalent, and it seems they are used ...
84
votes
5
answers
58k
views
How to access URL helper from rails module
I have a module with a function. It resides in /lib/contact.rb:
module Contact
class << self
def run(current_user)
...
end
end
end
I want to access the URL helpers like '...
84
votes
4
answers
34k
views
DDD - Persistence Model and Domain Model
I am trying to learn domain-driven design (DDD), and I think I got the basic idea. But there is something confusing me.
In DDD, are the persistence model and domain model different things? I mean, we ...
81
votes
2
answers
56k
views
Django - Model graphic representation (ERD) [closed]
I'm searching a way to represent my Django project model graphically.
Is there a "native" way to do this kind of ERD (diagram) ?
Update following @Etienne instructions
Here is an example of ...
80
votes
1
answer
27k
views
Rails 4: organize rails models in sub path without namespacing models?
Would it be possible to have something like this?
app/models/
app/models/users/user.rb
app/models/users/education.rb
The goal is to organize the /app/models folder better, but without having to ...
79
votes
7
answers
32k
views
Using multiple PostgreSQL schemas with Rails models
I have a PostgreSQL database for my Rails application. In the schema named 'public' the main Rails models tables are stored etc. I have created a 'discogs' schema which will have tables with names ...
79
votes
6
answers
88k
views
Django required field in model form
I have a form where a couple of fields are coming out as required when I don't want them too. Here is the form from models.py
class CircuitForm(ModelForm):
class Meta:
model = Circuit
...
77
votes
16
answers
59k
views
Rails before_validation strip whitespace best practices
I would like my User model to sanitize some input before before save. For now some simple whitespace stripping will do.
So to avoid people registering with "Harry " and pretend to be "Harry", for ...
77
votes
4
answers
126k
views
ForeignKey does not allow null values
I am using the Django REST Framework 2.0.
Here is my model class:
class Mission(models.Model):
assigned_to = models.ForeignKey('auth.User',
related_name='...
76
votes
11
answers
63k
views
All Levels of a Factor in a Model Matrix in R
I have a data.frame consisting of numeric and factor variables as seen below.
testFrame <- data.frame(First=sample(1:10, 20, replace=T),
Second=sample(1:20, 20, replace=T), Third=sample(...
76
votes
2
answers
13k
views
Fat models, skinny controllers and the MVC design pattern [closed]
I just read a blog post that explains MVC with a banking analogy. I have a few months of experience with web application development with an MVC framework (CakePHP), so I get the basics, but I began ...
75
votes
4
answers
85k
views
How can I set two primary key fields for my models in Django?
I have a model like this:
class Hop(models.Model):
migration = models.ForeignKey('Migration')
host = models.ForeignKey(User, related_name='host_set')
How can I have the primary key be the ...
73
votes
5
answers
40k
views
Should I make a DateRange object?
A few of my domain objects contain date ranges as a pair of start and end date properties:
public class Period {
public DateTime EffectiveDate { get; set; }
public DateTime ThroughDate { get; set;...