On Aug 9, 2011, at 5:43 PM, Chris Mear wrote:

> On 9 Aug 2011, at 22:08, Peter Bell wrote:
> As an aside: if your User has_one Profile, then normally you'd just access 
> that via User's instance method 'profile', which ActiveRecord provides. Is 
> there a particular reason you need to use a separate instance variable to 
> reference the profile?

Yeah - MongoMapper. I keep on running into issues with the has_one 
relationship, so I'm persisting a current_profile_id as an ObjectId and I need 
to handle the retrieval of the profile usine a Profile.find(current_profile_id)

> 
>> How does Ruby actually call instance variables? Does it make sense for me to 
>> def a method to overload the getter for the current_profile instance 
>> variable that looks to see if the actual instance variable exists and if not 
>> finds/creates it?
> 
> AFAIK, there isn't any way to hook into the access of an instance variable. 
> Overriding a getter method will only work if you only ever access that 
> instance variable via the getter method and not directly; unfortunately for 
> your case, ActiveSupport's Delegation module will generate code that accesses 
> your @current_profile instance variable directly.

Hmmm, OK, thanks

> As a first attempt, I'd probably just use ActiveRecord's after_initialize 
> hook in User to make sure I find/create a profile whenever a new User 
> instance is made.

I can't find support for after_initialize in MM unfortunately 
(http://mongomapper.com/documentation/plugins/callbacks.html), but I can look 
for the closest possible on the MM list.

> Not sure if that approach will play well with your factories, but then they 
> probably ought to be creating the Profile instance explicitly anyway.

That's certainly something I can do. So probably use a callback for the runtime 
code and just pass in the object in the factories. Seems like a way I can go - 
thanks!

Best WIshes,
Peter

> 
> Chris
> 
> -- 
> 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.
> 

-- 
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.

Reply via email to