9

我不确定我是否应该以这种方式更新记录,或者我是否遗漏了什么。

我有一个包含 5 列(不包括时间戳和 id)的表,其中 3 列是不同的,2 列将得到更新。我将找到或创建的 3 个不同之处是 room_id、日期和来源。其他 2 个是可用的价格和位置(每小时、每天等变化)

我的问题是,我应该先找到或创建记录,然后更新(或创建)价格和位置,还是可以一次完成?你可以看到我现在做的两种方式,我不确定它是否真的在做我期望的事情。

另外,像这样进行 find_and_create_by 有什么缺点吗?

谢谢

  private

  def self.parse_data(params,data)
    data.beds.each do |bed|
      room = Room.find_or_create_room(bed.title, params[:id])

      #find clones somehow
      #puts bed.nights.first.price
      bed.nights.each_with_index do |night,index|
        available = Available.find_or_create_by_room_id_and_bookdate_and_source(
          :room_id => room.id, 
          :bookdate => (params[:date].to_date)+index, 
          :source => data.class.to_s#,
          #:price => night.price
        )
        #available.price = night.price
        #available.spots = night.spots
        #available.save
      end

    end
4

3 回答 3

24

实际上,有一种方法无需任何黑客攻击。您可以使用 find_or_initialize_by 而不是 find_or_create_by 并通过点击设置更新的属性

Available.find_or_initialize_by_room_id_and_bookdate_and_source(
  room.id, 
  (params[:date].to_date)+index, 
  data.class.to_s#
).tap do |a|
  a.price = night.price
  a.spots = night.spots
end.save!

最初,这看起来很杂乱,但它完全按照您的要求进行。找到记录,如果没有找到则实例化它并更新属性。这可以称为“find_and_update_or_create_by”,幸运的是没有人这样做。;) 希望这有帮助。

于 2012-07-10T07:29:08.507 回答
15

这里有两种方法。

首先Available,您可以使用所需的确切方法进行扩展

def self.find_or_create_by_room_id_and_bookdate_and_source(room_id, bookdate, source, &block)
  obj = self.find_by_room_id_and_bookdate_and_source( room_id, bookdate, source ) || self.new(:room_id => room_id, :bookdate => bookdate, :source => source)
  yield obj
  obj.save
end

用法

Available.find_or_create_by_room_id_and_bookdate_and_source(room.id, (params[:date].to_date)+index, data.class.to_s) do |c|
  c.price = night.price
  c.spots = night.spots
end

这很尴尬。因此,为了更灵活,您可以创建使用魔法update_or_create_by...的方法:ActiveRecordmethod_missing

class ActiveRecord::Base
  def self.method_missing(method_id, *args, &block)
    method_name = method_id.to_s
    if method_name =~ /^update_or_create_by_(.+)$/
      update_or_create($1, *args, &block)
    else
      super
    end
  end
  def self.update_or_create(search, *args, &block)
    parameters = search.split("_and_")
    params = Hash[ parameters.zip(args) ]
    obj = where(params).first || self.new(params)
    yield obj
    obj.save
    obj
  end
end

所以现在你可以使用它了:

Available.update_or_create_by_id_and_source(20, "my_source") do |a|
  a.whatever = "coooool"
end
于 2011-04-07T11:10:26.117 回答
0

我认为最简单的方法是使用 Ruby 的tap方法,如下所示:

def self.parse_data(params,data)
  data.beds.each do |bed|
    room = Room.find_or_create_room(bed.title, params[:id])

    bed.nights.each_with_index do |night,index|
      Available.find_or_initialize_by(room_id: room.id).tap do |available|
        available.bookdate = (params[:date].to_date) + index
        available.source = data.class.to_s
        available.price = night.price
        available.save
      end
    end
  end
end

find_or_initialize_by查找或初始化记录,然后返回它。然后我们利用它,进行更新并将其保存到数据库中。

于 2020-09-03T07:36:43.150 回答