has_many通过关系抛出NoMethodError

时间:2019-02-05 21:56:02

标签: ruby-on-rails activerecord

我无法通过user.preferences.regions访问Preference

这是我的class Preference < ApplicationRecord belongs_to :user has_many :preference_regions has_many :regions, through: :preference_regions has_many :preference_home_airports has_many :home_airports, through: :preference_home_airports, source: :airport has_many :vacations accepts_nested_attributes_for :vacations end 类(忽略单个/多个偏好之间的混淆,我已经照顾好了)

User

这是我的require_relative './user/omniauth' class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable devise :omniauthable, omniauth_providers: %i[facebook] has_one :preferences, class_name: "Preference" has_many :vacations, through: :preferences has_many :regions, through: :preferences has_many :home_airports, through: :preferences after_create do |user| user.preferences = Preference.create end end

preferences.regions

我认为user.home_airports是多对多的事实,但另一种通过关系的ActiveRecord::Schema.define(version: 2019_02_05_181942) do create_table "preference_home_airports", force: :cascade do |t| t.bigint "preference_id" t.bigint "airport_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["airport_id"], name: "index_preference_home_airports_on_airport_id" t.index ["preference_id"], name: "index_preference_home_airports_on_preference_id" end create_table "preference_regions", force: :cascade do |t| t.bigint "preference_id" t.bigint "region_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["preference_id"], name: "index_preference_regions_on_preference_id" t.index ["region_id"], name: "index_preference_regions_on_region_id" end create_table "preferences", force: :cascade do |t| t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "regions", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "slug" end create_table "user_airports", force: :cascade do |t| t.integer "user_id" t.integer "airport_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "user_regions", force: :cascade do |t| t.integer "user_id" t.integer "region_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false 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 "provider" t.string "uid" t.string "first_name" t.string "last_name" 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 create_table "vacations", force: :cascade do |t| t.string "name" t.date "start_date" t.date "end_date" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "preference_id" end add_foreign_key "deal_origins", "airports" add_foreign_key "deal_origins", "deals", on_delete: :cascade add_foreign_key "preference_home_airports", "airports" add_foreign_key "preference_home_airports", "preferences", column: "preference_id" add_foreign_key "preference_regions", "preferences", column: "preference_id" add_foreign_key "preference_regions", "regions" end 可以正常工作。

有什么想法吗?

PS。这是我的架构的相关部分:

{{1}}

0 个答案:

没有答案