In the Rails unit test, how can I get a user device to load the associated profile?

In the documentation regarding Fixtures ( http://api.rubyonrails.org/classes/Fixtures.html ), they provide the following example of using label references for associations:

### in pirates.yml reginald: name: Reginald the Pirate monkey: george ### in monkeys.yml george: name: George the Monkey pirate: reginald 

So, following their example, I have a User model that has has_one: profile, a Profile model that belongs to user_ and tried to set up the lights for their example:

 ### in users.yml reginald: id: 1 login: reginald ### in profiles.yml reginalds_profile: id: 1 name: Reginald the Pirate user: reginald 

(Note: since my association is one-way, the user device does not have the association "profile: reginalds_profile" - this results in an error because the SQL table does not have the profile_id attribute.)

The problem is that in my unit tests everything seems to load correctly, but users (: reginald) .profile are always null. What am I missing?

+7
ruby-on-rails unit-testing fixtures
source share
2 answers

Based on the tadman’s assumption, I did a few more searches and found the answer elsewhere on this site, so I could also post it.

See the message under the heading Automatic associations in rubies on rail mounts.

Obviously, Rails finds related devices when you use labels (user: reginald) instead of identifiers (user_id: 1), by hashing the name and assuming the hash is the identifier. If you set an identifier for something specific, this will not work. But if you let Rails automatically assign identifiers, it uses this hash scheme. Thus, there is no key tidbit in the documentation for association labels - if you use labels, you should avoid using your own identifiers in matched fixtures. Luminaires that do not match the labels may still have any ID scheme you choose.

+18
source share

You may need to declare that you are loading all of these fixtures, since I do not believe that the instrument loader follows associations like this automatically. In some cases, you may need more than just:

 fixtures :all 

Actually announcing each of them:

 fixtures :users, :profiles 
+2
source share

All Articles