1

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
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • 2
    Is it possible to update the relationships section of the question to show the code relevant code from the models? It'll be easier to read that way, and might enable more insightful answers. – SRack Mar 21 '19 at 10:30
  • 1
    @SRack updated the models for you – user2012677 Mar 21 '19 at 11:28
  • I think you need the full join, check: https://stackoverflow.com/q/680141 – s3tjan Mar 21 '19 at 13:31
  • 1
    you have to properly define the reverse polymorphic association to addressable. i'm not sure though if it's even supported from rails to do such a polymorphic join – phoet Mar 21 '19 at 17:51

0 Answers0