2
create table foo  (id, name)
create table foo_config (id, foo_id, key, value)

insert into foo (1, 'name1')
insert into foo (2, 'name2')
insert into foo (3, 'name3')

insert into foo_config (1, 1, 'key1', 'val1')
insert into foo_config (2, 1, 'key2', 'val2')
insert into foo_config (3, 1, 'key3', 'val3')
insert into foo_config (4, 2, 'key1', 'val1')
insert into foo_config (5, 3, 'key2', 'val2')

How do I query for all foo's whose key1 = 'val1', in our case foo:1 and foo:2 will be listed here. How should I model / query for this, appreciate any pointers for the same.

Jason
  • 10,907
  • 18
  • 45
  • 66

1 Answers1

0

First you have to build the models, and set up the associations:

class Foo < ActiveRecord::Base
  has_many :foo_configs, :dependent => :destroy
end

class FooConfig < ActiveRecord::Base
  belongs_to :foo
end

And here is how you would set up the query:

Foo.where(:foo_configs => {:key => 'key1', :value => 'value1'}).includes(:foo_configs)