I believe you should be able to achieve the same just by overriding the
#user method in your class. I've commented on your last gist example with
an example code to make things more clear.

On Fri, Sep 7, 2012 at 2:28 PM, Joe Ferris <[email protected]> wrote:

> This looks useful to me. If you did implement this, it would be less rigid
> if you accepted a class instead of a class name, because that would allow
> you to pass anything that responds to #new.
>
> -Joe
>
>
> On Thursday, September 6, 2012 8:29:40 AM UTC-4, Kensodev wrote:
>>
>> Hey,
>>
>> For a while now, I am thinking about implementing a feature into Rails.
>> Since it's not a simple single liner, I thought I should get feedback on
>> it and maybe some guidelines before I continue on with coding it.
>>
>> The feature I would like to implement is nil_object pattern.
>>
>> Here's some code for example
>>
>> https://gist.github.com/**3655723 <https://gist.github.com/3655723>
>>
>> As you can see, the post belongs to a user and I am printing out the user
>> display name for every post.
>>
>> This is a pretty common thing in every rails app I ever saw, some
>> preload, some let it go N+1 without caring.
>>
>> The problem is, that if you don't have the user in the database, the view
>> code will fail on NoMethodError for NilClass.
>>
>> To Avoid this here's what I want to do:
>>
>> https://gist.github.com/**3655751 <https://gist.github.com/3655751>
>>
>> This way, if the database has no record for that user, it will load the
>> nil_user class and the code will now break.
>>
>> This can come in handy not only for the belongs_to but for the has_one
>> relation as well.
>>
>> So... This is the feature I want to implement, thoughts? feedback?
>> guidelines?
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-core/-/EX61uUxjIkQJ.
>
> 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-core?hl=en.
>



-- 
At.
Carlos Antonio

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" 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-core?hl=en.

Reply via email to