I’m looking for polymorphism/devise-invitable experts to help me out on a quite odd issue I’m running into. I’m trying to allow a User to be invited_by a Company. Default behaviour seems to fallback on the User model for this polyorphic field. Everything seems to be configured fine within the schema (I didn’t edit the initial migration from invitable). However this is what I get in console when I try to access my relation :
>> 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>
If anyone knows what could be the issue here :visage_légèrement_souriant: Also, this is what I get when I try to set my company manually
>> 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. this is my devise initializer
...
# 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
...
And my schema
...
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
...
Thanks! (modifié)