2012-09-07 137 views
0

使用寶石血統。祖先:從範圍跳過元素

如何從Category :: ActiveRecordRelation跳過自我元素或需要使用範圍?

= simple_form_for @category do |f| 
    = f.input :parent_id, collection: Category.roots 

是這樣的:

= f.input :parent_id, collection: Category.roots.except(@category) 

回答

2
= f.input :parent_id, collection: Category.roots.where("id <> ?", @category.id) 

或經由範圍

category.rb

scope :except, lambda{ |category| where("id <> ?", category.id) } 

然後

= f.input :parent_id, collection: Category.roots.except(@category) 
+0

我用這個,但我想這是從寶石血統實現的。 –