I can do this join:
User.joins(profile:[:address, :activity,:hobby])
However, I can not do this..
Address.joins(:profile)
I get:
ActiveRecord::ConfigurationError (Can't join 'Address' to association named 'profile'; perhaps you misspelled it?)
so I have to do this..
Address.where(addressable_type: "Profile").joins("INNER JOIN profiles ON addresses.addressable_id = profiles.id").all
However, my preferred join would be to join everything from address so I can run geocoder gem .near() method.
Address.joins(profile:[:activity,:hobby,:user])
error:
ActiveRecord::ConfigurationError (Can't join 'Address' to association named 'profile'; perhaps you misspelled it?)
I need to join Address to Profile, and then Profile to activity, hobby and user.
Models:
class User < ApplicationRecord
has_one :profile
end
class Profile < ApplicationRecord
belongs_to :user
has_one :address, as: :addressable
has_one :activity, as: :activityable
has_one :hobby, as: :hobbyable
end
class Address < ApplicationRecord
geocoded_by :full_address
after_validation :geocode, :if => :full_address
belongs_to :addressable, polymorphic: true
end
class Activity < ApplicationRecord
belongs_to :activityable, polymorphic: true
end
class Hobby < ApplicationRecord
belongs_to :hobbyable, polymorphic: true
end
schema
create_table "profiles", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
end
create_table "hobbies", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "hobbyable_type"
t.bigint "hobbyable_id"
end
create_table "addresses", force: :cascade do |t|
t.string "business_street"
t.string "business_city"
t.string "business_state"
t.string "business_country"
t.string "business_postal_code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "addressable_type"
t.bigint "addressable_id"
t.decimal "latitude", precision: 10, scale: 6
t.decimal "longitude", precision: 10, scale: 6
end
create_table "activities", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "activityable_type"
t.bigint "activityable_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end