Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
show all
- Defined in:
- lib/tasks/active_record_utils.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.create_or_update(options = {}, key = :id) ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/tasks/active_record_utils.rb', line 15
def self.create_or_update(options = {}, key = :id)
record_from_db = find(:first, :conditions=>{key => options[key.to_s]})
return record_from_db if record_from_db
options.delete "overwrite"
record = record_from_db || new
record.id = options["id"] if options["id"]
record.update_attributes!(options)
record
end
|
.create_or_update_for_site(options = {}, key = :name) ⇒ Object
4
5
6
7
8
|
# File 'lib/tasks/active_record_utils.rb', line 4
def self.create_or_update_for_site(options = {}, key = :name)
options["site"] = Site.find(:first, :conditions => {:name => options["site"]})
Page.current_site = options["site"]
create_or_update options, key
end
|
.create_or_update_for_sites(options = {}, key = :name) ⇒ Object
10
11
12
13
|
# File 'lib/tasks/active_record_utils.rb', line 10
def self.create_or_update_for_sites(options = {}, key = :name)
options["sites"] = options["sites"].collect { |site_name| Site.find(:first, :conditions => {:name => site_name}) }
create_or_update options, key
end
|
Instance Method Details
#create_or_update_with_attributes!(attributes) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/tasks/active_record_utils.rb', line 29
def create_or_update_with_attributes! attributes
puts attributes
return if existing_record?
update_attributes!(attributes)
end
|
#existing_record? ⇒ Boolean
25
26
27
|
# File 'lib/tasks/active_record_utils.rb', line 25
def existing_record?
(not id.nil?)
end
|