0

I have model with polymorhphic reference to two other models. I've also included distinct references per this article eager load polymorphic so I can still do model-specific queries as part of my .where clause. My queries work so I can search for scores doing Score.where(athlete: {foo}), however, when I try to do a .create, I get an error because the distinct reference alias seems to be blinding Rails of my polymorphic reference during validation.

Given that athletes can compete individually and as part of a team:

class Athlete < ApplicationRecord
  has_many :scores, as: :scoreable, dependent: :destroy
end

class Team < ApplicationRecord
  has_many :scores, as: :scoreable, dependent: :destroy
end

class Score < ApplicationRecord
 belongs_to :scoreable, polymorphic: true

 belongs_to :athlete, -> { where(scores: {scoreable_type: 'Athlete'}) }, foreign_key: 'scoreable_id'

 belongs_to :team, -> { where(scores: {scoreable_type: 'Team'}) }, foreign_key: 'scoreable_id'

  def athlete
    return unless scoreable_type == "Athlete"
    super
  end

  def team
    return unless scoreable_type == "Team"
    super
  end
end

When I try to do:

Athlete.first.scores.create(score: 5)

...or...

Score.create(score: 5, scoreable_id: Athlete.first.id, scoreable_type: "Athlete")

I get the error:

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: scores.scoreable_type

Thanks!

Bingo
  • 31
  • 1
  • 6
  • post here `schema.rb` file – blazpie Dec 03 '19 at 19:23
  • Unfortunately I can't post the schema.rb, but I can assure you that the scores.scoreable_id and scores.scoreable_type column are present, if that's the question. Something else you're looking for there? – Bingo Dec 03 '19 at 19:30
  • The error you're getting means literally that there is no column like `scoreable_type` so to help you debugging I need some data how the schema looks - maybe its just typo, maybe you missed something creating migration. One thing I would recommend is to try after deleting any other lines except `belongs_to :scoreable, polymorphic: true` from `Score` model – blazpie Dec 03 '19 at 21:05
  • also try to create by: `Score.scoreable.create(score: 5, scoreable: Athlete.first)` – blazpie Dec 03 '19 at 21:13
  • Going the deleted lines route, the only way it works is by deleting line `belongs_to :athlete, -> { where(scores: {scoreable_type: 'Athlete'}) }, foreign_key: 'scoreable_id'` or it's companion for the other polymorph. So, not only can I see that the db column in does exist, it will in fact work without the alias. And while I tried `Score.scoreable.create(score: 5, scoreable: Athlete.first)` and `Score.scoreables...` and `Score.create...`to no avail, I'm not clear how they would work...? Thanks! – Bingo Dec 03 '19 at 23:53
  • I was asking about deleting that lines because they're seems wrongly written to me. `belongs_to :athlete, -> { where(scores: {scoreable_type: 'Athlete'}) }, foreign_key: 'scoreable_id'` should be: `belongs_to :athlete, -> { joins(:scores).where(scores: {scoreable_type: 'Athlete'}) }, foreign_key: 'scoreable_id'` to work but of course is so badly wrote that its hurting my eyes. – blazpie Dec 04 '19 at 00:08
  • those scoped `belongs_to` can be easily substituted by scopes in `Score`: `scope :for_teams, -> { where(scorable_type: 'Team') }` – blazpie Dec 04 '19 at 00:17

1 Answers1

0

@blazpie, using your scoping suggestion worked for me.

"those scoped belongs_to can be easily substituted by scopes in Score: scope :for_teams, -> { where(scorable_type: 'Team') }

Bingo
  • 31
  • 1
  • 6