Rails Active Record – Get ids array from relation
A little bit more neat solution: Product.where(:colour => ‘blue’).pluck(:id) In recent versions of Rails the ids method can be used. Product.where(color: ‘blue’).ids
A little bit more neat solution: Product.where(:colour => ‘blue’).pluck(:id) In recent versions of Rails the ids method can be used. Product.where(color: ‘blue’).ids
Well, this could be a rails bug, but you can always generate your db structure with this: rake db:structure:dump This is going to generate an “#{Rails.env}.sql” file for you with your database structure in SQL.
In ActiveModel, valid? is defined as following: def valid?(context = nil) current_context, self.validation_context = validation_context, context errors.clear run_validations! ensure self.validation_context = current_context end So existing errors are cleared is expected. You have to put all your custom validations into some validate callbacks. Like this: validate :check_status def check_status errors.add(:status, “must be YES or NO”) unless … Read more
There is a select method in ARel, but you must use the correct table names (i.e. plural and beware if you have polymorphic models or if you’re using set_table_name or some other similar non-standard practice) @items = Item. select(‘users.name’, ‘users.created_at’, ‘orders.created_at’, ‘suppliers.name’, ‘agents.name’, ‘manufacturers.name’). where(:users => { :fulfilled => true }). includes(:orders => [:supplier, :agent], … Read more
You are on the right track, but I have run into a number of frustrating unexpected message errors when using rSpec, observers, and mock objects. When I am spec testing my model, I don’t want to have to handle observer behavior in my message expectations. In your example, there isn’t a really good way to … Read more
find returns an array, so you cannot use update_all. To solve the problem, I think you can use where, which returns an ActiveRecord::Relation, so the update_all should work: User.where(:id =>[23,45,68,123]).update_all(:is_active => true) http://apidock.com/rails/ActiveRecord/Relation/update_all I hope it helps…
You can use block with has_many to extend your association with methods. See comment “Use a block to extend your associations” here. Overriding existing methods also works, don’t know whether it is a good idea however. has_many :tags, :through => :taggings, :order => :name do def << (value) “overriden” #your code here super value end … Read more
if property.contract.nil? property.create_contract(some_attributes) else property.contract.update_attributes(some_attributes) end Should do the trick. When you have a has_one or belongs_to association then you get build_foo and create_foo methods (which are like Foo.new and Foo.create). If the association already exists then property.contract is basically just a normal active record object.
I would keep the :reject_if block but insert :_destroy => 1 into the attributes hash if your conditions are met. (This is useful in the cases where it’s not convenient to add _destroy to the form code.) You have to do an extra check to see if the record exists in order to return the … Read more
As you noticed, the only way is to use a custom validator. The :greater_than option should be an integer. The following code won’t work because both current and next release are available only at instance-level. class Project < ActiveRecord::Base validates_numericality_of :current_release validates_numericality_of :next_release, :greater_than => :current_release end The purpose of the greater_than option is to … Read more