ActiveRecord Arel OR condition

How can you combine 2 different conditions using logical OR instead of AND? NOTE: 2 conditions are generated as rails scopes and can’t be easily changed into something like where(“x or y”) directly. Simple example: admins = User.where(:kind => :admin) authors = User.where(:kind => :author) It’s easy to apply AND condition (which for this particular … Read more

How to use unscoped on associated relations in Rails3?

I have a default scope on products due to information security constraints. class Product < ActiveRecord::Base has_many :photos default_scope where(‘visible = 1’) end In my associated Photo model, however, I also have to find products that should not be visible. class Photo < ActiveRecord::Base belongs_to :product end my_photo.product In other cases, I can use unscoped … Read more

HTML5 ``

The WHATWG document for HTML5 says that the rel attribute must contain values that are space-separated, and then it gives a table of allowed values. The attribute’s value must be a set of space-separated tokens. The allowed keywords and their meanings… The list of allowed keywords for the link element does not include shortcut, but … Read more

Subqueries in activerecord

With SQL I can easily do sub-queries like this User.where(:id => Account.where(..).select(:user_id)) This produces: SELECT * FROM users WHERE id IN (SELECT user_id FROM accounts WHERE ..) How can I do this using rails’ 3 activerecord/ arel/ meta_where? I do need/ want real subqueries, no ruby workarounds (using several queries). Answer Rails now does this … Read more

How to do a LIKE query in Arel and Rails?

I want to do something like: SELECT * FROM USER WHERE NAME LIKE ‘%Smith%’; My attempt in Arel: # params[:query] = ‘Smith’ User.where(“name like ‘%?%'”, params[:query]).to_sql However, this becomes: SELECT * FROM USER WHERE NAME LIKE ‘%’Smith’%’; Arel wraps the query string ‘Smith’ correctly, but because this is a LIKE statement it doesnt work. How … Read more

Combine two ActiveRecord::Relation objects

Suppose I have the following two objects: first_name_relation = User.where(:first_name => ‘Tobias’) # ActiveRecord::Relation last_name_relation = User.where(:last_name => ‘Fünke’) # ActiveRecord::Relation is it possible to combine the two relations to produce one ActiveRecord::Relation object containing both conditions? Note: I’m aware that I can chain the wheres to get this behavior, what I’m really interested in … Read more

Want to find records with no associated records in Rails

Consider a simple association… class Person has_many :friends end class Friend belongs_to :person end What is the cleanest way to get all persons that have NO friends in ARel and/or meta_where? And then what about a has_many :through version class Person has_many :contacts has_many :friends, :through => :contacts, :uniq => true end class Friend has_many … Read more