Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/citier4/core_ext.rb,
lib/citier4/relation_methods.rb
Overview
Active Record Relation
Instance Method Summary collapse
-
#delete_all(conditions = nil) ⇒ Object
delete_all return the number of deleted records, and not anymore the succes or failure.
-
#load ⇒ Object
Causes the records to be loaded from the database if they have not been loaded already.
-
#relation_delete_all ⇒ Object
api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete delete_all(conditions = nil)) Deletes the records matching conditions without instantiating the records first, and hence not calling the destroy method nor invoking callbacks.
-
#relation_to_a ⇒ Object
TODO : remove to_a …, will be obsolete ? and doen’t work with rails 4.2 (ok for 4.1) to_a : Returns an array representation of obj.
- #to_a ⇒ Object
Instance Method Details
#delete_all(conditions = nil) ⇒ Object
delete_all return the number of deleted records, and not anymore the succes or failure.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/citier4/relation_methods.rb', line 22 def delete_all(conditions = nil) return relation_delete_all(conditions) if !@klass.acts_as_citier? return relation_delete_all(conditions) if conditions # deleted = true ids = nil c = @klass bind_values.each do |bind_value| if bind_value[0].name == "id" ids = bind_value[1] break end end ids ||= where_values_hash["id"] || where_values_hash[:id] where_hash = ids ? { :id => ids } : nil # debugger # ici, si on a un dictionaire avec id 10 # Product.delete 10 # Book.delete 10 # Dictionary.delete 10 # ne vont pas faire la meme chose. # Avec Product.delete 10, les Book::Writeable et Dictionary::Writeable ne sont pas déleter pour l'ID 10 # il faudrait pouvoir attaquer la boucle avec c = Dictionary et pas c = Product # ne marche que si on a que un ids et pas un tableau d'ids ... if ids.class == Fixnum # debugger c = @klass.find(ids).class end # deleted &= c.base_class.where(where_hash).relation_delete_all deleted = c.base_class.where(where_hash).relation_delete_all hierarchy_deleted ={} while c.superclass != ActiveRecord::Base # Refinery::Core::BaseModel # eoz ActiveRecord::Base if c.const_defined?(:Writeable) citier_debug("Deleting back up hierarchy #{c}") to_delete = c::Writeable.where(where_hash) hierarchy_deleted[c::Writeable.class.name] = to_delete.delete_all # deleted &= c::Writeable.where(where_hash).delete_all end c = c.superclass end citier_debug("Deleted_all : #{deleted}...#{hierarchy_deleted}") deleted end |
#load ⇒ Object
Causes the records to be loaded from the database if they have not been loaded already. You can use this if for some reason you need to explicitly load some records before actually using them. The return value is the relation itself, not the records.
Post.where(published: true).load # => #<ActiveRecord::Relation>
176 177 178 179 180 181 |
# File 'lib/citier4/core_ext.rb', line 176 def load puts "in load de citier4" exec_queries unless loaded? # debugger self end |
#relation_delete_all ⇒ Object
api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete delete_all(conditions = nil)) Deletes the records matching conditions without instantiating the records first, and hence not calling the destroy method nor invoking callbacks. This is a single SQL DELETE statement that goes straight to the database, much more efficient than destroy_all. Be careful with relations though, in particular :dependent rules defined on associations are not honored. Returns the number of rows affected.
19 |
# File 'lib/citier4/relation_methods.rb', line 19 alias_method :relation_delete_all, :delete_all |
#relation_to_a ⇒ Object
TODO : remove to_a …, will be obsolete ? and doen’t work with rails 4.2 (ok for 4.1) to_a : Returns an array representation of obj. For objects of class Object and others that don’t explicitly override the method, the return value is an array containing self. However, this latter behavior will soon be obsolete.
79 |
# File 'lib/citier4/relation_methods.rb', line 79 alias_method :relation_to_a, :to_a |
#to_a ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/citier4/relation_methods.rb', line 80 def to_a citier_debug("citier -> to_a") return relation_to_a if !@klass.acts_as_citier? records = relation_to_a c = @klass if records.all? { |record| record.class == c } return records end full_records = [] ids_wanted = {} # Map all the ids wanted per type records.each do |record| if record.class == c # We don't need to find the record again if this is already the correct one full_records << record next end ids_wanted[record.class] ||= [] ids_wanted[record.class] << record.id end # Find all wanted records ids_wanted.each do |type_class, ids| full_records.push(*type_class.find(ids)) end # Make a new array with the found records at the right places records.each do |record| full_record = full_records.find { |full_record| full_record.id == record.id } # debugger att = full_record.instance_variable_get(:@attributes) record.force_attributes(att, :merge => true, :clear_caches => false) # TODO record.force_attributes(full_record.instance_variable_get(:@attributes), :merge => true, :clear_caches => false) end return records end |