Mongoid strange ruby behavior #5776
-
I have a strange behavior using Ruby and Mongoid I have a Client model (simplified)
and and Utilisateur (user in french) that belong to one client:
My problem is that when a make a POST/PUT (create/update), even if I set the client manually, the object writting in the database doesnt hold a reference to my client. Here's my create method :
Note that on me POST result i got something that i would exepect:
Problem is that on my database I still got no client field:
I found a way to "solve" the problem by changing remove the foreign_key of client
Final problem is that my web app is dependent on another backend (NodeJS) who is using "client". Is there someting I could have missed because I did this with other class that look the same without problem. Any kind of help will be appraciated :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @CharlesPlantePulsar -- this is a tricky one. The problem is that the In this case, you may need to rename the association to something else, so that it doesn't collide with the foreign key. For example: class User
include Mongoid::Document
belongs_to :client_record, class_name: "Client", foreign_key: :client
end It's not ideal, but working with legacy systems can be like that. :/ |
Beta Was this translation helpful? Give feedback.
Hey @CharlesPlantePulsar -- this is a tricky one. The problem is that the
belongs_to
macro creates getter methods both for the association, and for the foreign key. In this case, both are named the same thing, so it creates theclient
accessor for the association first, and then overwrites it with theclient
accessor for the foreign key.In this case, you may need to rename the association to something else, so that it doesn't collide with the foreign key. For example:
It's not ideal, but working with legacy systems can be like that. :/