Class: FlexiRecord::Relationship

Inherits:
BaseRecord show all
Defined in:
lib/flexirecord.rb

Overview

A record representing a row of a database table which is used for cross (many-to-many) relations.

Direct Known Subclasses

FlexiRecordDemo::Rating

Instance Method Summary collapse

Methods inherited from BaseRecord

#[], #[]=, add_connected_references, add_many_to_one_reference, add_one_to_one_reference, add_read_option, after_select, autodetect_columns, columns, connection_pool, connection_pool=, db_execute, db_query, db_query1, #delete_from_cache, #destroy, #dup, #has_key?, #inspect, isolation_level, loader, lock, #method_missing, prepare_read_parameters, primary_columns, #read, read_option_value, reader, reader_attrs, #reload, #replace, #saved?, schema_name, schema_name!, schema_name=, select, select1, select_by_value_set, #set, set_loader, set_reader, set_setter, setter, sql, sql1, table, table_name, table_name!, table_name=, thread_connection_pool, thread_connection_pool=, #to_s, transaction, #transaction, transaction?, #update, use_connection, #used_columns

Constructor Details

#initialize(*arguments) ⇒ Relationship

Returns a new instance of Relationship.



1175
1176
1177
1178
# File 'lib/flexirecord.rb', line 1175

def initialize(*arguments)
  super
  self['void'] = nil unless self.has_key?('void')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FlexiRecord::BaseRecord

Instance Method Details

#saveObject

Instead of UPDATEing or INSERTing a value, depending on the state of the object, it is always replaced in the database. When the special attribute ‘void’ is set, the record will be destroyed instead of saved.



1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
# File 'lib/flexirecord.rb', line 1186

def save
  # TODO: improve efficiency, when a "REPLACE" command is available in PostgreSQL
  transaction(:read_committed) do
    self.class.lock(:share_row_exclusive)
    @saved =
      self.class.select_by_value_set(self.class.primary_columns, [self.class.primary_columns.collect { |column| self.read(column) }]).length > 0
    copy_primary_key
    if self.void
      return destroy
    else
      return super
    end
  end
end

#void?Boolean

Alias for the (field) method ‘void’. True, if the Relationship is void, and is to be removed, when calling ‘save’.

Returns:

  • (Boolean)


1181
1182
1183
# File 'lib/flexirecord.rb', line 1181

def void?
  self.void
end