lib/active_record_patch_new.rb
# create scope hash given association names and arguments the argument can be an number in case of an id or
# it is a hash in which the key points to the field of the association table and the value points to the value
# where we are looking for.
# E.g.
# scoped_by_user_id(6) creates the query :conditions => ["object_user_members.user_id IN (?)", 6], :include => [:users]
# scoped_by_user(:name => "jeroen") creates the query :conditions => ["users.name IN (?)", "jeroen"], :include => [:users]
# scoped_by_user(:like_name => "jeroen") creates the query :conditions => ["users.name LIKE ?", "%jeroen%"], :include => [:users]
create scope hash given association names and arguments the argument can be an number in case of an id or
it is a hash in which the key points to the field of the association table and the value points to the value
where we are looking for.
create scope hash given association names and arguments the argument can be an number in case of an id or it is a hash in which the key points to the field of the association table and the value points to the value where we are looking for .
def construct_scope_options_from_association_names(association_names, arguments, options = {})
				includes = association_names.map { |name| extract_association(name).to_sym }
				prepared_conditions = []
				prepared_arguments = []
				association_names.each_with_index do |name, idx|
					name = name.to_s
					association_name = extract_association(name)
					reflection = find_reflection(association_name)
					use_join_table = name.include?("_id")
					table_name = use_join_table ? ( [:has_many, :has_and_belongs_to_many].include?(reflection.macro) ? reflection.options[:join_table] + "." : "" ) :
																	 association_name.pluralize + "."
					field_name, prepared_arguments[idx] = (use_join_table ? [name, arguments[idx] ] : options.to_a.flatten[idx*2..idx*2+1] )
					field_name = field_name.to_s 
					if field_name.include?("like_")
						field_name = field_name.gsub("like_", '')                              
						prepared_arguments[idx] = "%" + prepared_arguments[idx] + "%"          
						use_like = true
					end
					prepared_conditions << (use_like ? ["#{table_name}#{field_name} LIKE ?"] : ["#{table_name}#{field_name} IN (?)"] )
				end
				{:conditions => [prepared_conditions.join(" AND "), *prepared_arguments], :include => includes}
			end
def construct_scope_options_from_association_names ( association_names , arguments , options = { } ) includes = association_names . map { | name | extract_association ( name ) . to_sym } prepared_conditions = [ ] prepared_arguments = [ ] association_names . each_with_index do | name , idx | name = name . to_s association_name = extract_association ( name ) reflection = find_reflection ( association_name ) use_join_table = name . include? ( "_id" ) table_name = use_join_table ? ( [ :has_many , :has_and_belongs_to_many ] . include? ( reflection . macro ) ? reflection . options [ :join_table ] + "." : "" ) : association_name . pluralize + "." field_name , prepared_arguments [ idx ] = ( use_join_table ? [ name , arguments [ idx ] ] : options . to_a . flatten [ idx * 2 .. idx * 2 + 1 ] ) field_name = field_name . to_s if field_name . include? ( "like_" ) field_name = field_name . gsub ( "like_" , '' ) prepared_arguments [ idx ] = "%" + prepared_arguments [ idx ] + "%" use_like = true end prepared_conditions << ( use_like ? [ "#{table_name}#{field_name} LIKE ?" ] : [ "#{table_name}#{field_name} IN (?)" ] ) end { :conditions => [ prepared_conditions . join ( " AND " ) , * prepared_arguments ] , :include => includes } end
create scope hash given association names and arguments the argument can be an number in case of an id or
it is a hash in which the key points to the field of the association table and the value points to the value
where we are looking for.
create scope hash given association names and arguments the argument can be an number in case of an id or it is a hash in which the key points to the field of the association table and the value points to the value where we are looking for .
# the field name is either the given name (when a name followed by _id is given, or it is a hash) # we want it to be a string # Handle the situation where the name has like_ prefix meaning we need to introduce wildcards # remove the like_ prefix # add wildcards
association_names
<not_specific>
arguments
<not_specific>
options
<not_specific>