On Jan 9, 12:44 pm, ivanpoval <[email protected]> wrote: > On Jan 8, 5:20 pm, Frederick Cheung <[email protected]> > wrote: > > > On Jan 7, 5:03 pm, IAmNan <[email protected]> wrote:> At the point > > > > If I stop in the debugger at > > > self.buyer_id = self.buyer.id # Won't work without this line, seems > > > un-DRY or worse > > > I'd try stepping through the assignment in self.buyer = foo. In theory > > you should end up in BelongsToProxy#replace which in turn should be > > setting buyer_id > > Shouldn't this happen only after you save the initial object? > I mean that at the time you do self.buyer = User.create ... it creates > a new associated object in the memory as an attribute of self, but > buyer_id still doesn't have that information because at the time you > fetched a self object into the memory it just didn't exist. > Depends on the type of relationship and whether what you're setting the object to is already saved. Here we're using the return value of User.create, so it should be a saved object, so you just need to take the id of that object and stick it in buyer_id. If the relationship had been as one then we'd have to wait until the Order object had an id.
Fred I think if you do self.save (in your example it should be only for > testing purposes because it's already inside the save callback) and > self.reload after self.buyer = User.create you should see a value > assigned. > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

