Answer the question
In order to leave comments, you need to log in
Active Record, scope and nesting of the third level?
Hello!
There is a User model . It contains the Estatehas_many :estates, dependent: :destroy
Model . It has a polymorphic relationship
has_one :location, as: :locatable, dependent: :destroy
scope :type, -> (type) { User.includes(:estates).where("estates.re_type" => type) }
class CreateLocations < ActiveRecord::Migration
def change
create_table :locations do |t|
t.string :address
t.decimal :lat, {:precision=>10, :scale=>6}
t.decimal :lng, {:precision=>10, :scale=>6}
t.references :locatable, polymorphic: true, index: true
t.timestamps null: false
end
add_index :locations, [:lat, :lng]
end
end
User.where(estates: { location: { name: "there" } })
SQLite3::SQLException: no such column: real_estates.locatable_id: SELECT "users".* FROM "users" WHERE "real_estates"."locatable_id" = '---
:address: !ruby/object:Arel::Nodes::BindParam {}'
User.joins(estates: :location).where(location: {address: "London"})
SQLite3::SQLException: no such column: location.address: SELECT "users".* FROM "users" INNER JOIN "estates" ON "estates"."user_id" = "users"."id" INNER JOIN "locations" ON "locations"."locatable_id" = "estates"."id" AND "locations"."locatable_type" = ? WHERE "location"."address" = 'London'
Answer the question
In order to leave comments, you need to log in
First, try a hash entry, like
User.where(estates: { location: { name: "there" } })
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question