Answer the question
In order to leave comments, you need to log in
How to change the id of the current model instance for the has_one connection?
The documentation says for has_one
:primary_key
Specify the method that returns the primary key used for the association. By default this is id.
As I understand it, when a model uses this relationship, then the referenced table is searched for by primary_key, which by default is the id of this model and which in turn has a has_one relationship
class Model_A
belongs_to model_b
end
class Model_B
has_one model_a
end
SELECT "model_a".*
FROM "model_a" WHERE "model_a"."model_b_id" = $1
ORDER BY "model_a"."id" ASC LIMIT 1
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question