ruby-on-rails - rails : how to check if find_or_in

我有从 CSV 更新数据库中数据的方法。

csv_text = File.read("#{Rails.root}/db/seed/books.csv")
csv = CSV.parse(csv_text, :headers => :first_row)
csv.each { |row|
row = row.to_hash.with_indifferent_access.to_hash.symbolize_keys
row.delete_if { |key, value| value.nil? }
book = Book.find_or_initialize_by_name(row[:name])
book.update_attributes!(row)

我如何检查记录是否会被创建或更新或者什么都没有发生?

最佳答案

要检查 book 是否已更改,有 changed? :

book.changed?

要检查 book 是否是新记录,有 new_record? :

book.new_record?

关于ruby-on-rails - rails : how to check if find_or_initialize is updating/creating/skipping?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10852313/

相关文章:

groovy - Groovy 中的 JUnit @Before 方法排序

PHP Google Calendar API - 更新事件

asp.net-mvc-2 - 无法将类型 'double?' 隐式转换为 'double'

email - 使用 ssl 的 Yandex smtp 设置

sql - 如何优化此 SQL 查询(使用索引)?

css - 向右浮动和小时对齐

php - MySQLi 查询以获取表中的最大 id?

sql-server-2008 - 一个选择语句中的不同条件

caching - 什么是暖缓存?

string - 将字符串/字符转换为 uint8