Class: ActiveRecord::Associations::HasReferencesToAssociation

Inherits:
HasManyAssociation
  • Object
show all
Defined in:
lib/serialized_attributes/has_references_to.rb

Instance Method Summary collapse

Instance Method Details

#construct_sqlObject



13
14
15
16
17
# File 'lib/serialized_attributes/has_references_to.rb', line 13

def construct_sql
  @finder_sql = "#{@reflection.quoted_table_name}.id IN (#{ids * ', '})"
  @finder_sql << " AND (#{conditions})" if conditions
  @counter_sql = @finder_sql
end

#create(attrs = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/serialized_attributes/has_references_to.rb', line 31

def create(attrs = {})
  if attrs.is_a?(Array)
    attrs.collect { |attr| create(attr) }
  else
    create_record(attrs) do |record|
      yield(record) if block_given?
      self.ids = (ids << record.id) if record.save
    end
  end
end

#create!(attrs = {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/serialized_attributes/has_references_to.rb', line 42

def create!(attrs = {})
  create_record(attrs) do |record|
    yield(record) if block_given?
    record.save!
    self.ids = (ids << record.id)           
  end
end

#delete_records(records) ⇒ Object



27
28
29
# File 'lib/serialized_attributes/has_references_to.rb', line 27

def delete_records(records)
  self.ids = ids - records.map(&:id)
end

#idsObject



5
6
7
# File 'lib/serialized_attributes/has_references_to.rb', line 5

def ids
  (@owner["#{@reflection.name.to_s.singularize}_ids"] ||= []).map(&:to_i)
end

#ids=(array = []) ⇒ Object



9
10
11
# File 'lib/serialized_attributes/has_references_to.rb', line 9

def ids=(array = [])
  @owner["#{@reflection.name.to_s.singularize}_ids"] = array.map(&:to_i)
end

#insert_record(record, force = false, validate = true) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/serialized_attributes/has_references_to.rb', line 19

def insert_record(record, force = false, validate = true)
  load_target
  set_belongs_to_association_for(record)
  result = !record.new_record? || (force ? record.save! : record.save(:validate => validate))
  self.ids = (ids + [record.id]) if result
  result
end