Thanks for the tip, Manasi. This didn't solve the problem. After
defining them as you suggest I do the following:
user = User.new
user.save!
membership = user.memberships.create
role = membership.create_role
Now the interesting thing is that user.memberships.find(:first) has no
role_id, but membership does. I'm not sure what this means, but I'll
explore it further. I suspect my problem might be cached records,
but:
I don't know if this is the problem, but I think you forgot Role
has_many :memberships.
I don't think methods on the Role class should affect this in anyway.
I should only define the association if I benefit from the methods it
provides, right? Or are there other effects I should be aware of?
Anyways, I gave this a try and the results are the same.
I'll admit, though, that in this case I don't really see that the
Membership model is any use. Why aren't you just connecting User and
Role directly?
Fair question.
You're probably right that this should be redesigned. The reasons I
ended up with this are:
1) Originally I had planned to have a has_many relationship between
user and role. When my plan changed I was lazy and didn't change
models/schema.
2) Personal taste. I don't like to have empty fields in my database
by design. If a record exists, all of the fields should have a
value. This design lets a user have 0 or 1 roles and I test for this
by looking for the existance of a record in the join table. I don't
know why this bothers me and it's probably irrational.
3) The example is somewhat contrived, though it does mimic an
application I'm writing. I left out all of the fields for these
tables except the IDs so that I could demostrate the problem here.
This may be a dumb question, but what version of Rails are you on? I
know that versions earlier than 2.2.2 had some difficulties with
has_one :through...
Yeah, I was looking at some related bug fixes, but I think they are
all incorporated. I'm using 2.3.2. There are a couple things I'll
try later tonight when I have a chance to look again:
First, the debugger is giving me something strange in the
AssociationProxy class in the reload method. After calling reset (it
uses AssociationCollection#reset) @loaded == true according to the
debugger. If loaded really is true, it might expliain this, but I'm
still under the assumption that I'm misinterpreting the debugger, or
the debugger is wrong, because otherwise I misunderstand inheritance
in ruby and there's a bug in rails (this seems less likely). I
haven't been able to duplicate this with a smaller repro, but I'll try
again tonight.
Second, I think it's very likely that my scenario isn't all that
common. And it's very likely that the association implementation
wasn't designed with this in mind. I'm running into this in a unit
test. Instead of creating models in memory, I'll load up data from
the test DB, and I'm guessing user.roles suddenly starts working.
Then at least I know I can work around the problem with test fixtures.
In the meantime, if anyone else has an idea of what's going on, let me
know.
When I do this, user.role no longer returns nil, it returns the role
as expected. I think things weren't cached quite right, and for
whatever reason I couldn't force a proper reload. Maybe a bug in
rails?
Yep, working with AssociationProxy stuff in either the debugger or the
console is nasty. Things have a tendency to happen without being told
to (especially loading the target).
Also, have you tried calling membership.save in your original example?
The code you show will set the FK in membership (via
membership.create_role), but it's not on the DB until you save it...
Specifically, "The create_association method returns a new object of
the associated type. This object will be instantiated from the passed
attributes, and the link through its foreign key will be set. In
addition, the associated object will be saved (assuming that it passes
any validations)."
Is the guide wrong about this being saved? In any case, I actually
did try this with the build_association followed by a .save! and got
exactly the same results.