Module: CompositePrimaryKeys::ActiveRecord::Base::CompositeInstanceMethods
- Defined in:
- lib/composite_primary_keys/base.rb
Instance Method Summary collapse
-
#clone ⇒ Object
Returns a clone of the record that hasn’t been assigned an id yet and is treated as a new record.
-
#id ⇒ Object
(also: #ids)
A model instance’s primary keys is always available as model.ids whether you name it the default ‘id’ or set it to something else.
-
#id=(ids) ⇒ Object
Sets the primary ID.
-
#id_before_type_cast ⇒ Object
:nodoc:.
-
#quoted_id ⇒ Object
:nodoc:.
- #to_param ⇒ Object
Instance Method Details
#clone ⇒ Object
Returns a clone of the record that hasn’t been assigned an id yet and is treated as a new record. Note that this is a “shallow” clone: it copies the object’s attributes only, not its associations. The extent of a “deep” clone is application-specific and is therefore left to the application to implement according to its need.
85 86 87 88 89 90 91 |
# File 'lib/composite_primary_keys/base.rb', line 85 def clone attrs = self.attributes_before_type_cast self.class.primary_keys.each {|key| attrs.delete(key.to_s)} self.class.new do |record| record.send :instance_variable_set, '@attributes', attrs end end |
#id ⇒ Object Also known as: ids
A model instance’s primary keys is always available as model.ids whether you name it the default ‘id’ or set it to something else.
48 49 50 51 |
# File 'lib/composite_primary_keys/base.rb', line 48 def id attr_names = self.class.primary_keys CompositeIds.new(attr_names.map { |attr_name| read_attribute(attr_name) }) end |
#id=(ids) ⇒ Object
Sets the primary ID.
70 71 72 73 74 75 76 77 78 |
# File 'lib/composite_primary_keys/base.rb', line 70 def id=(ids) ids = ids.split(ID_SEP) if ids.is_a?(String) ids.flatten! unless ids.is_a?(Array) and ids.length == self.class.primary_keys.length raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids" end [primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)} id end |
#id_before_type_cast ⇒ Object
:nodoc:
58 59 60 |
# File 'lib/composite_primary_keys/base.rb', line 58 def id_before_type_cast #:nodoc: raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::NOT_IMPLEMENTED_YET end |
#quoted_id ⇒ Object
:nodoc:
62 63 64 65 66 67 |
# File 'lib/composite_primary_keys/base.rb', line 62 def quoted_id #:nodoc: [self.class.primary_keys, ids]. transpose. map {|attr_name,id| quote_value(id, column_for_attribute(attr_name))}. to_composite_ids end |
#to_param ⇒ Object
54 55 56 |
# File 'lib/composite_primary_keys/base.rb', line 54 def to_param id.to_s end |