Skip to content

Instantly share code, notes, and snippets.

@fedesoria
Last active December 27, 2015 05:19
Show Gist options
  • Save fedesoria/7273215 to your computer and use it in GitHub Desktop.
Save fedesoria/7273215 to your computer and use it in GitHub Desktop.
What I'm I doing wrong?
class User
has_many :parameter_values, as: :valuable
has_many :parameters,-> { uniq }, :through => :parameter_values
end
class ParameterValue
belongs_to :parameter
belongs_to :valuable, :polymorphic => true
end
class Parameter
has_many :parameter_values
end
user = User.create()
paramater_value = ParameterValue.create(parameter_id: 6, valuable_id: 1, valuable_type: "User")
user.parameter_values << parameter_value
user.parameter_values # [#<ParameterValue id: 1, parameter_id: 6, valuable_id: 1, valuable_type: "User">]
user.parameters # [] wtf?
user.parameters.explain # Last part of the query ["valuable_id", nil] should be ["valuable_id", 1]
=> EXPLAIN for: SELECT DISTINCT "parameters".* FROM "parameters" INNER JOIN "parameter_values" ON "parameters"."id" = "parameter_values"."parameter_id" WHERE "parameter_values"."valuable_id" = $1 AND "parameter_values"."valuable_type" = $2 [["valuable_id", nil], ["valuable_type", "User"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment