Find all records which have a count of an association greater than zero

1) To get Projects with at least 1 vacancy: Project.joins(:vacancies).group(‘projects.id’) 2) To get Projects with more than 1 vacancy: Project.joins(:vacancies).group(‘projects.id’).having(‘count(project_id) > 1’) 3) Or, if Vacancy model sets counter cache: belongs_to :project, counter_cache: true then this will work, too: Project.where(‘vacancies_count > ?’, 1) Inflection rule for vacancy may need to be specified manually?

Update the value of a field in database by 1 using codeigniter

From the documentation: set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. So this should work to pass the increment statement directly to the database: $this->db->set(‘usage’, ‘usage+1’, FALSE); $this->db->where(‘tag’, ‘java’); $this->db->update(‘tags’); Because it’s not escaped, if you’re using a variable instead of a fixed … Read more

Rails – Validate Presence Of Association?

You can use validates_presence_of http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of class A < ActiveRecord::Base has_many :bs validates_presence_of :bs end or just validates http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates class A < ActiveRecord::Base has_many :bs validates :bs, :presence => true end But there is a bug with it if you will use accepts_nested_attributes_for with :allow_destroy => true: Nested models and parent validation. In this topic you … Read more