How to remove items from Activerecord query resultset?
Use reject as in Model.find_all_by_xxx().reject { |r| r.something? }
Use reject as in Model.find_all_by_xxx().reject { |r| r.something? }
All migrations are wrapped in transaction by default so that they can be rolled back when they are failed. You need to use disable_ddl_transaction when you want to do something that can not excute inside a transaction. You can check Transactional Migrations section of Migration document for example.
This is a fairly recent addition to Rails, so it may not be covered in the book you mention. You can read about it in the migration section of Rails Guides. When you generate using, say, rails generate model Thing name post:references … the migration will create the foreign key field for you, as well … Read more
I don’t think that default Rails validators will do the trick here, you can do this though: validate :validate_wdays def validate_wdays if !wdays.is_a?(Array) || wdays.any?{|d| !(0..6).include?(d)} errors.add(:wdays, :invalid) end end
Once the relation has been loaded, you can use regular array methods. find is actually a very interesting method here – if block is specified, it will be delegated to the relation target: @blogs.find {|b| b.id == 1}
From here If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true Or in Rails3 way validates :field, :inclusion => {:in … Read more
Here’s how I’m using ActiveRecord outside of Rails: #!/usr/bin/ruby require ‘active_record’ require ‘mysql2’ # or ‘pg’ or ‘sqlite3’ ActiveRecord::Base.establish_connection( adapter: ‘mysql2’, # or ‘postgresql’ or ‘sqlite3’ database: ‘DB_NAME’, username: ‘DB_USER’, password: ‘DB_PASS’, host: ‘localhost’ ) # Note that the corresponding table is ‘orders’ class Order < ActiveRecord::Base end Order.all.each do |o| puts “o: #{o.inspect}” end
Try relation = Model.scoped. That will give you the relation instead of the actual results.
No, it’s not a bad idea. Many people do it and I couldn’t live without it in large applications. There are two ways of doing it: The first is to just move your models. You will, however, have to tell Rails to load the wayward models (as it won’t know where they are). Something like … Read more
To rewrite the SQL query you’ve got in your question, I think it should be like the following (though I’m having a hard time fully visualizing your model relationships, so this is a bit of guesswork): RagaContextApplicantsSong. joins(:raga_contest_applicants => [:raga_content_rounds], :contest_cat). group(‘raga_contest_rounds.contest_cat_id’) …such that the joins method takes care of both of the two joins … Read more