Questions tagged [model]

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

Filter by
Sorted by
Tagged with
43 votes
6 answers
8k views

Ruby on Rails: Is it better to validate in the model or the database?

Is it generally better practice (and why) to validate attributes in the model or in the database definition? For (a trivial) example: In the user model: validates_presence_of :name versus in the ...
William Jones's user avatar
43 votes
1 answer
40k views

AttributeError: 'ManyRelatedManager' object has no attribute 'add'? I do like in django website but got this error

for item in data: category_id = item['category_id'] del item['category_id'] category = Category.objects.get(pk=category_id) code = item['code'] try: article = Article....
Totty.js's user avatar
  • 15.7k
42 votes
4 answers
169k views

How to set a hidden value in Razor

I know that what I'm trying to do is bad idea, but I have specific constrains for now. I have multiple sites, using one and the same MVC3 code base. For one of them the requirement is to hide some ...
Sunny Milenov's user avatar
42 votes
3 answers
77k views

Call a model method in a Controller

I'm have some difficulties here, I am unable to successfully call a method which belongs to a ProjectPage model in the ProjectPage controller. I have in my ProjectPage controller: def index @...
sidney's user avatar
  • 2,714
42 votes
4 answers
20k views

Share types between client and server

I'm working on a project with a Node.js, Express.js & TypeScript backend (REST API) and a React, Redux & TypeScript frontend. In the backend I have created some types for example: models/...
Steven Soekha's user avatar
42 votes
5 answers
19k views

guidelines for where to put classes in Rails apps that don't fit anywhere

I'm wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don't fit in any of the default directories (controllers/models etc.). I'm talking ...
Kuba Suder's user avatar
  • 7,677
41 votes
7 answers
48k views

Rails Model without a table

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can't find it on google. My question is: How can I put the colors in ...
user993460's user avatar
40 votes
3 answers
27k views

How to get the app a Django model is from?

I have a model with a generic relation: TrackedItem --- genericrelation ---> any model I would like to be able to generically get, from the initial model, the tracked item. I should be able to ...
Bite code's user avatar
  • 588k
40 votes
4 answers
30k views

How to implement Active Record inheritance in Ruby on Rails?

How to implement inheritance with active records? For example, I want a class Animal, class Dog, and class Cat. How would the model and the database table mapping be?
andrisetiawan's user avatar
40 votes
2 answers
29k views

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own. Given the three model classes with their associations: # application_form....
Ash's user avatar
  • 25.1k
40 votes
6 answers
12k views

rails model has_many of itself

I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event....
Alexander Forbes-Reed's user avatar
40 votes
3 answers
25k views

Rails Internationalization (I18n) in model validations: Possible or not?

I have the following validation in a model: validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever') It seems that the translation does not work in ...
TomDogg's user avatar
  • 3,908
39 votes
5 answers
30k views

Check if a OneToOne relation exists in Django

Now I'm using django 1.6 I have two models relates with a OneToOneField. class A(models.Model): pass class B(models.Model): ref_a = models.OneToOneField(related_name='ref_b', null=True) ...
Alfred Huang's user avatar
39 votes
3 answers
13k views

Usage of MVVM in iOS

I'm an iOS developer and I'm guilty of having Massive View Controllers in my projects so I've been searching for a better way to structure my projects and came across the MVVM (Model-View-ViewModel) ...
Isuru's user avatar
  • 31k
39 votes
6 answers
9k views

Rails Models: how would you create a pre-defined set of attributes?

I'm trying to figure out the best way to design a rails model. For purposes of the example, let's say I'm building a database of characters, which may have several different fixed attributes. For ...
Andrew's user avatar
  • 42.8k
38 votes
4 answers
48k views

Tensorflow: How to convert .meta, .data and .index model files into one graph.pb file

In tensorflow the training from the scratch produced following 6 files: events.out.tfevents.1503494436.06L7-BRM738 model.ckpt-22480.meta checkpoint model.ckpt-22480.data-00000-of-00001 ...
Rafal's user avatar
  • 479
37 votes
4 answers
17k views

What is the Rails3 version of errors.add_to_base?

I want to write a custom validation in Rails 3, but following this example I get: 'ActiveModel::DeprecatedErrorMethods.add_to_base' call is deprecated in Rails 3.0 What is the rails3 version of: ...
Marius Butuc's user avatar
  • 18.1k
37 votes
6 answers
97k views

Get Laravel Models with All Attributes

Is there a way to retrieve a model in Laravel with all the attributes, even if they're null? It seems to only return a model with the attributes that aren't null. The reason for this is that I have a ...
kenshin9's user avatar
  • 2,295
37 votes
1 answer
16k views

model.matrix generates fewer rows than original data.frame

Why doesn't a model matrix necessarily have the same number of rows as the data frame? mergem = model.matrix(as.formula(paste(response, '~ .')), data=mergef) dim(mergef) # [1] 115562 71 dim(...
xyzzyrz's user avatar
  • 16.2k
36 votes
7 answers
49k views

In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation?

Currently, in my ModelFactory.php, I have: $factory->define(App\Reply::class, function (Faker\Generator $faker) { return [ 'thread_id' => 1, 'user_id' => 1, 'body' => $faker-...
Simon Suh's user avatar
  • 10.7k
36 votes
7 answers
61k views

This expression is not callable. Type 'Number' has no call signatures

Hi I'm new in Typescript, I have an object-type variable in which might be different values from different types or nested object. now my question is that how can I define a model for this object to ...
Mohammad's user avatar
  • 671
36 votes
5 answers
114k views

Update Table Using Laravel Model

I've got a table for a sports team. The record shows the team selection and some other information. I want to update the record with the team selection. My model is thus: class Selection extends ...
danjswade's user avatar
  • 567
36 votes
9 answers
25k views

Django self-recursive foreignkey filter query for all childs

I have this model with a self referencing Foreign Key relation: class Person(TimeStampedModel): name = models.CharField(max_length=32) parent = models.ForeignKey('self', null=True, blank=True,...
Ahsan's user avatar
  • 11.8k
36 votes
4 answers
20k views

Required Attribute on Generic List Property

Is it possible to put a [Required] attribute onto a List<> property? I bind to a generic list on POST and was wondering if I could make ModelState.IsValid() fail if the property has 0 items in it?
Nick Reeve's user avatar
  • 1,698
36 votes
2 answers
17k views

How to have two models reference each other Django

I have the following code: class Game(models.Model): title = models.CharField(max_length=50) summery = models.CharField(max_length=500) key = models.IntegerField() pin = models....
sinθ's user avatar
  • 11.3k
36 votes
4 answers
28k views

Rails: Serializing objects in a database?

I'm looking for some general guidance on serializing objects in a database. What are serialized objects? What are some best-practice scenarios for serializing objects in a DB? What attributes do you ...
keruilin's user avatar
  • 17.2k
36 votes
1 answer
6k views

Best approach to model validation in PHP? [closed]

I've learned that there are often many ways to solve one programming problem, each approach typically having its own benefits and negative side affects. What I'm trying to determine today is the best ...
Jonathan's user avatar
  • 18.7k
35 votes
4 answers
19k views

Rails: belongs_to vs has_one

A bit of a newbie question on rails associations. I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status ...
Matt Briggs's user avatar
  • 41.7k
34 votes
5 answers
50k views

How can my django model DateField add 30 days to the provided value?

as the title suggests. I want to add 30 days to the DateField field. This is auto populated on creation of record using auto_now_add=True Any ideas how to go about doing this? Thanks
dotty's user avatar
  • 41k
34 votes
11 answers
43k views

Models in the Zend Framework

What are some of the ways you have implemented models in the Zend Framework? I have seen the basic class User extends Zend_Db_Table_Abstract and then putting calls to that in your controllers: $foo ...
rg88's user avatar
  • 20.8k
34 votes
3 answers
28k views

rails validate in model that value is inside array

I have a form where i pass a field named :type and i want to check if it's value is inside an array of allowed types so that no one is allowed to post not-allowed types. the array looks like @...
stergosz's user avatar
  • 5,778
34 votes
10 answers
22k views

How do I validate that two values do not equal each other in a Rails model?

I have a User model, which has an email and a password field. For security, these may not be equal to each other. How can I define this in my model?
user avatar
34 votes
7 answers
36k views

Django model instances primary keys do not reset to 1 after all instances are deleted

I have been working on an offline version of my Django web app and have frequently deleted model instances for a certain ModelX. I have done this from the admin page and have experienced no issues. ...
pj2452's user avatar
  • 905
34 votes
5 answers
41k views

MVC :: What is a model?

I am at a point where I must make a decision about models. I am aware that models are what you use to do all your database manipulation. But are models restricted to this? Are they meant only for ...
Hailwood's user avatar
  • 91k
34 votes
4 answers
32k views

Kohana 3: Example of model with validation

I find examples and tutorials about models and about validation. And I places that say the validation (or most of it at least) should be in the model, which I agree with. But I can't any examples or ...
Svish's user avatar
  • 155k
33 votes
15 answers
90k views

Yii model to array?

How can I convert the result of Trips::model()->findAll() to an array?
Joeeee's user avatar
  • 1,256
33 votes
6 answers
49k views

Laravel 5 Models Folder

I have a directory called models inside app/ where I place all my model classes. Now I want to change the directory where the following command outputs the generated classes php artisan make:model ...
Alan's user avatar
  • 2,609
33 votes
6 answers
14k views

Rails: how to require at least one field not to be blank

I know I can require a field by adding validates_presence_of :field to the model. However, how do I require at least one field to be mandatory, while not requiring any particular field? thanks in ...
deb's user avatar
  • 12.6k
32 votes
4 answers
44k views

How to set initial data for Django admin model add instance form?

How can I set an initial value of a field in the automatically generated form for adding a Django model instance, before the form is displayed? I am using Django 1.3.1. My model is the following: ...
Martijn de Milliano's user avatar
32 votes
5 answers
17k views

What is DOM? (summary and importance)

What is the Document Object Model (DOM)? I am asking this question because I primarily worked in .NET and I only have limited experience, but I often hear more experienced developers talk about/...
VoodooChild's user avatar
  • 9,784
31 votes
2 answers
42k views

Real example of TryUpdateModel, ASP .NET MVC 3

I can't understand, how to use TryUpdateModel and save the MVC architecture at the same time. If I am not mistaken, work with datacontexts must be in the Model. So, such code var db=new ...
Sir Hally's user avatar
  • 2,338
31 votes
5 answers
111k views

Laravel how to add a custom function in an Eloquent model?

I have a Product model class Product extends Model { ... public function prices() { return $this->hasMany('App\Price'); } ... } I want to add a function which will ...
Harrison's user avatar
  • 2,630
31 votes
2 answers
14k views

Rails put validation in a module mixin?

Some validations are repetitive in my models: validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true validates :name_seo, :length => { :minimum => 2 },...
Victor Pudeyev's user avatar
31 votes
4 answers
14k views

difference between scaffold and model in Rails

What's the difference between generating a scaffold and generating a model in Rails? What are the advantages/disadvantages of doing either?
jazzyfresh's user avatar
31 votes
6 answers
21k views

Rails: change column type, but keep data

I have a model with a column of type integer which I want to convert to type string. Now I'm looking for the best way to change the column type without losing the data. Is there a painless way to ...
blissini's user avatar
  • 545
31 votes
3 answers
36k views

Better option to check if a particular instance exists django

Which of the two is a better and efficient option to check if an instance exists. There can only be one record returned at most. 1) Use the filter option and see if it exists: x = MyObject.objects....
Aswin Murugesh's user avatar
31 votes
2 answers
20k views

Access fields in Django intermediate model

I'm creating a Person Group and Membership as described in Django docs for intermediate model. class Person(models.Model): name = models.CharField(max_length=128) def __unicode__(self): ...
pablo's user avatar
  • 2,789
31 votes
6 answers
22k views

Laravel models: Where are model properties?

(I come from Visual Studio + Entity Framework background and trying to locate equivalent functionality in Laravel + Eloquent) In EF and Visual Studio, we add a new Model to our application and just ...
dotNET's user avatar
  • 34.4k
30 votes
4 answers
31k views

Override django's model delete method for bulk deletion

I'm overriding Django's model delete method in order to delete orphan files in the disk for image fields, something like this: class Image(models.Model): img = models.ImageField(upload_to=...
Kilian Perdomo Curbelo's user avatar
30 votes
4 answers
25k views

How to set default value of TextBox empty string instead of null

I may be out of date, but one principle I adhere to is avoid nulls as much as possible. However what I have found is that for a strongly typed view in which the user inputs the properties of an ...
arame3333's user avatar
  • 10k