Find record whose field 'name' not contained within any other record

Posted by charlie on Stack Overflow See other posts from Stack Overflow or by charlie
Published on 2014-05-31T02:13:57Z Indexed on 2014/05/31 3:26 UTC
Read the original article Hit count: 153

I have a model Foo with a String bar and a String name. Some records' bar contain the name of other records in them. This is intentional.

I want to find the "root Foo" records - that is, the ones where their name do not appear in the bar records of any other Foo records.

Example:

Foo
id: 1
name: 'foo1'
bar:  'something something'

id: 2
name: 'foo2'
bar:  'foo1 something'

id: 3
name: 'foo3'
bar:  'foo1, foo4'

My method root_foos would return foo2 and foo3 since their names do not appear in any bar string.

edit: I don't want to use a relation or foreign key here - just this method.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3