query: tag:through

色々奥が深いと思った(not http://0xcc.net/misc/bad-knowhow.html 笑)

■前提:記事←関連テーブル→ユーザというモデル関係がある。
ruby>>

has_many :throughのパターン

class Article< ActiveRecord::Base
has_many :articles_users
has_many :users, :through => :articles_users
end

class User < ActiveRecord::Base
has_many :articles_users, :dependent => :destroy
has_many :articles, :through => :articles_users
end

関連テーブルを実テーブルで持つ

class ArticlesUsers < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
<<ruby

■関連テーブルの操作

あるユーザの関連をごそっと別の関連に切り替えたいような場合!

User.articles_users.destroy_allして作り直すという方法もあるが
User.articles_users.replace()という便利なメソッドがある。

AWD第二版(AgileWebDevelopment)で言うとP308。
「○orders.replace(order1,...)
この顧客に関連付けられた注文のセットを、新しいセットに置き換える。現在の子のセットと新しいセットの違いを検出し、それに応じてデータベースの変更を最適化する。」という奴。

ruby>>
/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_collection.rb

  # Replace this collection with +other_array+
  # This will perform a diff and delete/add only records that have changed.
  def replace(other_array)
    other_array.each { |val| raise_on_type_mismatch(val) }

    load_target
    other   = other_array.size < 100 ? other_array : other_array.to_set
    current = @target.size < 100 ? @target : @target.to_set

    @owner.transaction do
      delete(@target.select { |v| !other.include?(v) })
      concat(other_array.select { |v| !current.include?(v) })
    end
  end

<<ruby
これは便利。

1.配列を放り込むだけ

「Replace this collection with +other_array+」ということで検索結果、あるいはセッションなどで生成した新しい関連の配列を放り込むだけで使える。

2.以前の関連との差分をチェックして差分だけ反映してくれる。

「This will perform a diff and delete/add only records that have changed.」ということで必要な差分だけチェックして必要なdelete/addをしてくれる。

ちなみにdeleteだと「Railsレシピブック」のP157「Entryオブジェクトobjectとの関連を削除する。Entryオブジェクトobjectの外部キー(blog_id)をNULLにし、関連を削除する。複数の参照元オブジェクトを同時に指定できる。」にある通り、関連テーブルの実レコードは削除されない(外部キーがNULLになるだけ)。関連テーブルにごみが残って気持ち悪い場合は上記モデルのように「:dependent => :destroy」を付けると物理的に削除される。

posted by ysakaki ysakaki on Fri 22 Aug 2008 at 15:39 with 0 comments
Contents rssrss
Leading Smart TV App Development Companies | USA | 4 Way Technologies
Apple TV Developer | TVOS Development | USA | 4 Way Technologies
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Value Transformer
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Tags
through
Comments rssrss
https://businessrost.ru/bitrix/redirect.php?goto=http%3A%2F%2Feximha.ch%2Fblog%2Findex.php%3Fid%3Dz85yj914%26abuse%3D1636 https://businessrost.ru/bitrix/redirect.php?goto=http%3A%2F%2Feximha.ch%2Fblog%2Findex.php%3Fid%3Dz85yj914%26abuse%3D1636: I have to thank you for the efforts you have put ... 04/26 06:43
http://ldm.sakura.ne.jp/balu+bbbs/album/album.cgi?mode=detail&no=210 http://ldm.sakura.ne.jp/balu+bbbs/album/album.cgi?mode=detail&no=210: Aw, this was an incredibly good post. Taking the t... 04/25 18:19
https://www.bigbeargum.com/2019/02/15/ambrose-redmoon/ https://www.bigbeargum.com/2019/02/15/ambrose-redmoon/: Hello would you mind letting me know which hosting... 04/25 17:39
http://zeroken.jp/1978td/album/album.cgi?mode=detail&no=20 http://zeroken.jp/1978td/album/album.cgi?mode=detail&no=20: Asking questions are genuinely fastidious thing if... 04/25 17:26
https://natucci-estetica.com/?attachment_id=1376 https://natucci-estetica.com/?attachment_id=1376: Hello there, You have done a fantastic job. I wi... 04/25 17:05
https://www.waimaoz.com/23019.html?unapproved=5151&moderation-hash=a14b2a811444fc3059edb7c1441d076e https://www.waimaoz.com/23019.html?unapproved=5151&moderation-hash=a14b2a811444fc3059edb7c1441d076e: Good day! Do you use Twitter? I'd like to follow y... 04/25 16:50
https://bysystem.ru/process-proizvodstva-kartonnyh-korobok-i/ https://bysystem.ru/process-proizvodstva-kartonnyh-korobok-i/: Требовалось производство картонных коробок в сжа... 04/25 15:33
https://marialionsa.ru/proizvodstvo-kartonnyh-korobok-i-upakovki/ https://marialionsa.ru/proizvodstvo-kartonnyh-korobok-i-upakovki/: Документооборот четкий, все закрывающие акты предо... 04/25 15:05