Search code examples
rubypostgresqlsequel

Can't query one_to_many relationship with Sequel gem?


I'm using the Sequel gem on a Postgres database (using Ruby).

I have a one_to_many relationship between the class "Home" and the class "Person". So, many people are in a home.

I've got my class schema defined as such:

class Home < Sequel::Model
  one_to_many :person
end

class Person < Sequel::Model
  many_to_one :home
end

I've got a selection of people (p = Person.where(:age => 20)) and now I'm trying to query the homes that those people are in, using Home.where(:persons => p). However, I'm getting the error column "persons" does not exist.

Any ideas here?


Solution

  • First of all change one_to_many :persons in your model. And collect your homes with this:

    p.map { |person| person.home }