Better option to check if a particular instance exists django

An even better approach would be to use .exists() to check if a particular instance exists or not. MyObject.objects.filter(someField=someValue).exists() # return True/False From the .exists() docs: It returns True if the QuerySet contains any results, and False if not. This tries to perform the query in the simplest and fastest way possible, but it does … Read more

Rails put validation in a module mixin?

module Validations extend ActiveSupport::Concern included do validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true end end The validates macro must be evaluated in the context of the includer, not of the module (like … Read more

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

You can use: validate :any_present? def any_present? if %w(field1 field2 field3).all?{|attr| self[attr].blank?} errors.add :base, “Error message” end end EDIT: updated from original answer for Rails 3+ as per comment. But you have to provide field names manually. You could get all content columns of a model with Model.content_columns.map(&:name), but it will include created_at and updated_at … Read more

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

I wouldn’t call it an issue. This is default behaviour for many database systems. Basically, the auto-increment counter for a table is persistent, and deleting entries does not affect the counter. The actual value of the primary key does not affect performance or anything, it only has aesthetic value (if you ever reach the 2 … Read more

Could not find the association problem in Rails

In the ApplicationForm class, you need to specify ApplicationForms’s relationship to ‘form_questions’. It doesn’t know about it yet. Anywhere you use the :through, you need to tell it where to find that record first. Same problem with your other classes. So # application_form.rb class ApplicationForm < ActiveRecord::Base has_many :form_questions has_many :questions, :through => :form_questions end … Read more

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

Good question – i don’t have a concrete answer for you but I recommend checking out this post – http://blog.codeclimate.com/blog/2012/02/07/what-code-goes-in-the-lib-directory/ – be sure to read through all the comments on a current project i have a ton of non-ActiveRecord objects under app/models, it works but not ideal i put ‘re-useable’ non application specific code under … Read more

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

JamesO is correct – it looks like your Category.articles field has a through relationship. Assuming that your models at least resemble the following class Article(models.Model): name = models.CharField(max_length=128) class Category(models.Model): name = models.CharField(max_length=128) articles = models.ManyToManyField(Article, through=”Membership”) class Membership(models.Model): article = models.ForeignKey(Article) category = models.ForeignKey(Category) author = models.CharField() then to add an Article to a … Read more

Create Ruby on Rails views (only) after controllers and models are already created

rails g scaffold User –migration=false –skip The –skip means to skip files that already exist. (The opposite is –force.) If you don’t want helpers, –helpers=false. Sample output after deleting my User views: invoke active_record identical app/models/user.rb invoke test_unit identical test/unit/user_test.rb skip test/fixtures/users.yml route resources :users invoke scaffold_controller identical app/controllers/users_controller.rb invoke erb exist app/views/users create app/views/users/index.html.erb … Read more

tech