-1

私はそのようなモデルを持っています

class Ads::Posting < ActiveRecord:Base
  has_one :child, class_name: 'Ads::Posting', foreign_key: :posting_id
  belongs_to :parent, class_name: 'Ads::Posting', foreign_key: :posting_id
end

子なしですべての投稿を取得するスコープを作成する必要があります。それを行う方法はありますか?

4

2 に答える 2

0

句 に aLEFT JOINと の制限を使用してこれを行うことができると思います。where

scope :my_scope, 
  joins("LEFT JOIN postings ON postings.ad_id = ads.id").where("postings.id IS NULL")
于 2013-01-14T16:52:17.150 に答える
0

そのモデルは意味がありません!

子は自動的に親になります。

正確には何を作りたいですか?木?

次に、移行に追加する必要があります

t.integer :parent_id, null: false

そしてあなたのモデルで

has_one :child, class_name: 'Ads::Posting', foreign_key: :parent_id

より深くするために、見てください

https://github.com/stefankroes/ancestry

多分それはあなたを助けています。

于 2013-01-14T16:51:31.637 に答える