0

オブジェクトを作成する前に、オブジェクトの存在を確認するための最良の方法は何でしょうか。

検索クエリを保存したいのですが、データベースに既に存在する場合、毎回新しいクエリを作成するのは意味がありません。カウンターをインクリメントして、最も一般的なクエリに関する統計を取得したいと思います。

私の最大の問題は、オブジェクトがすでにベースにあるかどうかを確認する方法です。テーブル全体を引き出したり、オブジェクトの1つが新しいオブジェクトと等しいかどうかをチェックしたりせずに、これをチェックする魔法の関数があるかどうかを知りたいですか?

また、それは実際にフォーム上にありfind_or_create( myObject )、すべての属性が同じであるかどうかを確認し(そして、各クエリオブジェクトに多くの属性があるかどうか)、idもちろん無視する必要があります。

だからこれは私が使用できないことを意味しますfind_or_create_by_region_id_and_departement_id_and_other_attr

ああ、私はRails3.2.6で作業しています:)

これを行う方法はありますか?

編集して、検索クエリのテーブル構造を追加します。

# == Schema Information
#
# Table name: search_queries
#
#  id                 :integer(4)      not null, primary key
#  region_id          :integer(4)
#  departement_id     :integer(4)
#  ville              :string(255)
#  environnement_id   :integer(4)
#  etoiles_min        :integer(4)
#  etoiles_max        :integer(4)
#  tente              :boolean(1)
#  caravane           :boolean(1)
#  campingcar         :boolean(1)
#  mobilhome          :boolean(1)
#  chalet             :boolean(1)
#  bungalow           :boolean(1)
#  yourte             :boolean(1)
#  roulotte           :boolean(1)
#  tipi               :boolean(1)
#  autres             :boolean(1)
#  wifi               :boolean(1)
#  restaurant         :boolean(1)
#  epicerie           :boolean(1)
#  depotpain          :boolean(1)
#  laverie            :boolean(1)
#  espace_aqua        :boolean(1)
#  club_enfant        :boolean(1)
#  multisport         :boolean(1)
#  loc_velo           :boolean(1)
#  acces_pmr          :boolean(1)
#  animaux            :boolean(1)
#  naturiste          :boolean(1)
#  l_campingqualite   :boolean(1)
#  l_qualitetourisme  :boolean(1)
#  l_tourismehandicap :boolean(1)
#  l_ecotourisme      :boolean(1)
#  l_normandiequalite :boolean(1)
#  l_ancv             :boolean(1)
#  created_at         :datetime        not null
#  updated_at         :datetime        not null
#
4

4 に答える 4

3

first_or_create/first_or_initializeオブジェクトがない場合は、オブジェクトを作成するために使用できます。

query = Query.where(...).first_or_initialize

#do something with the query (increment counter, store parameters)

query.save!
于 2012-08-02T15:03:46.450 に答える
0

Rails は幸いなことに、既存のレコードの検証に関して多くの甘い取引をサポートしています。

他の人が示唆しているように、チェックしたいデータに適用できるexistsメソッドがあります。

また、validates_uniqueness_of は、レコード全体を検証できます。

個人的には、より多くのパラメーターをチェーンすることもできる find_or_create メソッドが気に入っています。

楽しむ!

于 2012-08-02T15:07:33.633 に答える
0

So as the Model.where method doesn't accept an object but only a hash and this hash mustn't contain an id field, what you need to use is this :

@query = SearchQuery.where( queryObject.instance_values["attributes"].except("id")      ).first_or_create

Where queryObject is the object you need to check the existence of

Thank you davidrac for your help :)

Although I'm kind of disapointed that Rails doesn't come with this built-in, I mean there are so many great things to a lot of tasks quicker than anywhere else and not this ?

于 2012-08-03T09:44:38.720 に答える
0

これを行う最善の方法は、exists? see this for more information を使用することです。

于 2013-12-26T20:19:38.143 に答える