提问人:Julien 提问时间:7/18/2023 更新时间:7/18/2023 访问量:24
DeviseInvitable 多态invited_by关联始终回退到用户
DeviseInvitable Polymorphic invited_by association always fallbacks to User
问:
我正在寻找多态性/设计吸引人的专家来帮助我解决我遇到的一个非常奇怪的问题。我正在尝试允许用户invited_by公司。默认行为似乎回退到此多色字段的用户模型上。 在架构中,一切似乎都配置良好(我没有编辑 invitable 的初始迁移)。但是,当我尝试访问我的关系时,这是我在控制台中得到的:
>> current_or_guest_user.invited_by_id
=> 5141
>> current_or_guest_user.invited_by_type
=> "Company"
>> current_or_guest_user.invited_by
=> #<User id: 5141, email: "guest_103459b2-37e6-4444-814a-f057600c5c43@example...", created_at: "2023-01-31 17:24:01.611300000 +0100", updated_at: "2023-01-31 17:24:01.611300000 +0100", first_name: nil, last_name: nil, birthdate: nil, cellphone: nil, landline: nil, role: nil, stripe_id: nil, iban_id: nil, managed_company_id: nil, source: "organic", work_situation: nil, added_all_financial_resources: nil, authentication_token: nil, guest: true, landlord_legal_status: nil, notify_by_email: true, address_id: nil>
如果有人知道这里可能有什么问题:visage_légèrement_souriant: 此外,这是我尝试手动设置公司时得到的
>> current_or_guest_user.invited_by = current_or_guest_user.invited_by_type.constantize.find(current_or_guest_user.invited_by_id)
=> #<Company id: 5141, name: "Le courtier généreux", created_at: "2023-07-05 13:33:40.455689000 +0200", updated_at: "2023-07-18 09:13:59.716326000 +0200", siret: 99938374625363, legal_name: "", company_type: "broker", headquarter_id: nil, brokered_by_id: nil, depozen_surety_bond_fixed_fee_cents: 2500, home_insurance_tenant_occupant_fixed_fee_cents: 0, home_insurance_non_occupant_owner_fixed_fee_cents: 0, home_insurance_occupant_owner_fixed_fee_cents: 0, has_all_employments: false, available_products: ["depozen_surety_bond", "home_insurance_tenant_occupant", "home_insurance_non_occupant_owner", "concierge"], has_configured_available_products: true, email: "", slug: "", providers: {"depozen_surety_bond"=>"wakam", "home_insurance_tenant_occupant"=>"wakam", "home_insurance_non_occupant_owner"=>"mila", "concierge"=>"selectra"}, address_id: nil, available_group_agreements: [], has_configured_available_group_agreements: false, branding: "white_label">
>> current_or_guest_user.invited_by
=> #<User id: 5141, email: "guest_103459b2-37e6-4444-814a-f057600c5c43@example...", created_at: "2023-01-31 17:24:01.611300000 +0100", updated_at: "2023-01-31 17:24:01.611300000 +0100", first_name: nil, last_name: nil, birthdate: nil, cellphone: nil, landline: nil, role: nil, stripe_id: nil, iban_id: nil, managed_company_id: nil, source: "organic", work_situation: nil, added_all_financial_resources: nil, authentication_token: nil, guest: true, landlord_legal_status: nil, notify_by_email: true, address_id: nil>
P.S. 这是我设计的初始化器
...
# The class name of the inviting model. If this is nil,
# the #invited_by association is declared to be polymorphic.
# Default: nil
# config.invited_by_class_name = nil
# The foreign key to the inviting model (if invited_by_class_name is set)
# Default: :invited_by_id
# config.invited_by_foreign_key = :invited_by_id
...
还有我的架构
...
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", precision: nil
t.datetime "remember_created_at", precision: nil
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "invitation_token"
t.datetime "invitation_created_at", precision: nil
t.datetime "invitation_sent_at", precision: nil
t.datetime "invitation_accepted_at", precision: nil
t.integer "invitation_limit"
t.string "invited_by_type"
t.bigint "invited_by_id"
t.integer "invitations_count", default: 0
t.string "first_name"
t.string "last_name"
t.date "birthdate"
t.string "cellphone"
t.string "landline"
t.integer "role"
t.string "stripe_id"
t.bigint "iban_id"
t.bigint "managed_company_id"
t.integer "source", default: 0
t.string "confirmation_token"
t.datetime "confirmed_at", precision: nil
t.datetime "confirmation_sent_at", precision: nil
t.string "unconfirmed_email"
t.integer "work_situation"
t.boolean "added_all_financial_resources"
t.string "authentication_token", limit: 30
t.boolean "guest", default: false
t.integer "landlord_legal_status"
t.boolean "notify_by_email", default: true
t.bigint "address_id"
t.index ["address_id"], name: "index_users_on_address_id"
t.index ["authentication_token"], name: "index_users_on_authentication_token", unique: true
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email"
t.index ["iban_id"], name: "index_users_on_iban_id"
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
t.index ["invited_by_type", "invited_by_id"], name: "index_users_on_invited_by"
t.index ["managed_company_id"], name: "index_users_on_managed_company_id"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
...
谢谢!(修改)
答: 暂无答案
评论
User.reflect_on_association :invited_by