sferik/rails_admin — Issue #908: Hide some records in multiselect-field
Author: tmoe
Created: 2012-01-03 15:09:55+00:00
State: open

Hi :)

First of all let me thanks for this great administrationinterface-solution! :) Its really helpfull in my current project!
I work with ruby on rails since three month so my question is maybe "easy" to solve ;)

Imagine i have a model Topic with a self-reference to display a father-child relationship:

Topic.rb

```

class Topic < ActiveRecord::Base

belongs_to :topic, :foreign_key => "parent_id"
end

```

Now i get a additional model called "Person". A Person can have many Topics
Person.rb

```

class Person < ActiveRecord::Base

has_many :topics
end

```

(These relationship is a bit more complex with polymorphic n:m-relationships but that is not very important )

Now the Question:

I'm able to add Topics to a Person with the create/edit-Form of the Person, no problem. The exact exercise is: Only leafs (that Topics which have no children) should be visible in the Person-Formular.
How can i solve that? :/ The (not optimized... ;) ) SQL-Command to get the leafs:

```
select * from topics t1 where (select count(id) from topics t2 where t2.parent_id = t1.id) == 0

```

I have also create a Method in Topic.rb

```

def is_leaf?
return Topic.find(:all, :select => "id, parent_id", :conditions => " parent_id = #{self.id}").empty?
end

```

But i dont know how to use my... "constructs..." :/
Maybe is the solution something like https://github.com/sferik/rails_admin/wiki/Associations-scoping ?
Please help :-/

Thank u very much :)