Присоединяется в одну сторону, но не в другую - PullRequest
1 голос
/ 21 марта 2019

Я могу сделать это объединение:

User.joins(profile:[:address, :activity,:hobby])

Однако я не могу сделать это ..

Address.joins(:profile)

Я получаю:

ActiveRecord:: ConfigurationError (Не удается присоединить 'Address' к ассоциации с именем 'profile'; возможно, вы ошиблись?)

, поэтому я должен сделать это ..

Address.where(addressable_type: "Profile").joins("INNER JOIN profiles ON addresses.addressable_id = profiles.id").all

ОднакоМое предпочтительное соединение должно состоять в том, чтобы объединить все с адреса, чтобы я мог запустить метод geocoder gem .near ().

Address.joins(profile:[:activity,:hobby,:user]) 

error:

ActiveRecord :: ConfigurationError (Can 't присоединить 'Address' к ассоциации с именем 'profile'; возможно, вы ошиблись?)

Мне нужно присоединить Address к профилю, а затем Profile к активности, хобби и пользователю.

Модели:

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

схема

  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
...