我可以进行此加入:
User.joins(profile:[:address, :activity,:hobby])
但是,我做不到。
Address.joins(:profile)
我得到:
ActiveRecord :: ConfigurationError(无法加入“地址”进行关联 命名为“个人资料”;也许您拼错了?)
所以我必须这样做。
Address.where(addressable_type: "Profile").joins("INNER JOIN profiles ON addresses.addressable_id = profiles.id").all
但是,我的首选联接是将地址中的所有内容联接起来,这样我就可以运行地址解析器gem .near()方法。
Address.joins(profile:[:activity,:hobby,:user])
ActiveRecord :: ConfigurationError(无法加入“地址”进行关联 命名为“个人资料”;也许您拼错了?)
我需要将“地址”加入“个人资料”,然后将“个人资料”加入活动,爱好和用户。
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