Ruby on Rails
Sunday, September 23, 2018
Hello all,
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:
hello_steve:
to: steve
from: jana
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:
# user.rb
has_many :sent_messages, class_name: 'Message', foreign_key: 'from'
has_many :received_messages, class_name: 'Message', foreign_key: 'to'
# message.rb
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.
Any ideas?
Thank you
Josef
-- 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:
hello_steve:
to: steve
from: jana
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:
# user.rb
has_many :sent_messages, class_name: 'Message', foreign_key: 'from'
has_many :received_messages, class_name: 'Message', foreign_key: 'to'
# message.rb
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.
Any ideas?
Thank you
Josef
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ebf60074-64dc-4e4b-b98e-3769c407d577%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment