Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingIndexChecker violation on polymorphic association #185

Closed
kleinjm opened this issue Mar 22, 2023 · 3 comments
Closed

MissingIndexChecker violation on polymorphic association #185

kleinjm opened this issue Mar 22, 2023 · 3 comments

Comments

@kleinjm
Copy link

kleinjm commented Mar 22, 2023

Violation: MissingIndexChecker fail Project locations associated model should have proper index in the database

Schema

  create_table "locations", force: :cascade do |t|
    t.decimal "latitude"
    t.decimal "longitude"
    t.string "owner_type", null: false
    t.bigint "owner_id", null: false
    t.index ["owner_type", "owner_id"], name: "index_locations_on_owner"
  end

Location model has belongs_to(:owner, polymorphic: true)

Project model

  has_many(
    :locations,
    inverse_of: :owner,
    foreign_key: :owner_id,
    dependent: :destroy,
  )

I'm not sure what else is missing. There is no column pointing to the location model from the project model. Any ideas what the violation means? Thanks in advance and thanks again for the gem!

@djezzzl
Copy link
Owner

djezzzl commented Apr 14, 2023

Hi @kleinjm,

Thank you for using the gem and reporting the issue!

I'm sorry it took me long to look at it.

I'm not sure what else is missing. There is no column pointing to the location model from the project model. Any ideas what the violation means?

Looking at your association, I think the message is correct because your association is not defined as polymorphic.

has_many(
    :locations,
    inverse_of: :owner,
    foreign_key: :owner_id,
    dependent: :destroy,
  )

It should be

has_many :locations, as: :owner, dependent: :destroy

You can read more about polymorphic associations here: https://guides.rubyonrails.org/association_basics.html#polymorphic-associations.

Please let me know if that did or didn't help. Feel free to reopen the issue if needed.

@djezzzl djezzzl closed this as completed Apr 14, 2023
@kleinjm
Copy link
Author

kleinjm commented Apr 14, 2023

That did it! Thanks a lot for the pointer. I confused inverse_of with as

@djezzzl
Copy link
Owner

djezzzl commented Apr 16, 2023

I'm glad that it helped @kleinjm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants