1
我有以下引入nokogiri rake任務在我的Rails(3.1)應用:Rails 3.1創建記錄而不是更新?
desc "Import incoming calls"
task :fetch_incomingcalls => :environment do
# Logs into manage.dial9.co.uk and retrieved list of incoming calls.
require 'rubygems'
require 'mechanize'
require 'logger'
# Create a new mechanize object
agent = Mechanize.new
# Load the dial9 website
page = agent.get("https://manage.dial9.co.uk/login")
# Select the first form
form = agent.page.forms.first
form.username = 'username
form.password = 'password'
# Submit the form
page = form.submit form.buttons.first
# Click on link called Call Logs
page = agent.page.link_with(:text => "Call Logs").click
# Click on link called Incoming Calls
page = agent.page.link_with(:text => "Incoming Calls").click
# Output results to file
# output = File.open("output.html", "w") { |file| file << page.search("tbody td").text.strip }
# Add each row to a new call record
page = agent.page.search("table tbody tr").each do |row|
next if (!row.at('td'))
time, source, destination, duration = row.search('td').map{ |td| td.text.strip }
Call.create!(:time => time, :source => source, :destination => destination, :duration => duration)
end
end
的時間值是在表中的第一行是每次通話唯一的(因爲我們只能收到一個電話在同一時間)。
我想要做的是使用時間值作爲我的通話記錄的唯一標識符。
因此,當刮屏幕,它會「更新」現有的調用(這不會改變,但它是我唯一可以想到的只有導入新調用的方式)。
如果我將其設置爲:
Call.find_all_by_time(nil).each do |call|
然後:
call.update_attribute(:time, time)
然後,它會更新現有的記錄,但我希望它導入已不在我們的數據庫記錄 - 基於時間值。
任何幫助表示讚賞!
這正是我的意思 - 謝謝! – dannymcc 2012-01-13 12:33:31