I cannot seem to find a way how to reference named fixtures in cases where Rails fixtures auto-id magic is not working.
Imagine a model Message with attributes from and to as integers indicating users IDs.
Now the following fixture won’t work:
text: Hello there!
(Given steve and jana are defined in users.yml. They work for other fixtures defined with standard Rails conventions.)
The thing is I need to reference them here (or their ID).
Thing I tried was to put in place Rails belongs_to:
has_many :sent_messages, class_name: ‘Message’, foreign_key: ‘from’
has_many :received_messages, class_name: ‘Message’, foreign_key: ‘to’
belongs_to :recipient, class_name: ‘User’, foreign_key: ‘to’
belongs_to :author, class_name: ‘User’, foreign_key: ‘from’
Unfortunately Rails won’t pick it up and all messages fixtures end up with from and to as ‘0’.
Note that the association works, just not for fixtures.