# Getting a user from a model (sort of a noob) (resend sorry)

**URL:** <https://www.ruby-forum.com/t/getting-a-user-from-a-model-sort-of-a-noob-resend-sorry/94916>\
**Category:** Rails\
**Created:** [January 10, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/getting-a-user-from-a-model-sort-of-a-noob-resend-sorry/94916 "2006-01-10T15:39:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 10, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/getting-a-user-from-a-model-sort-of-a-noob-resend-sorry/94916/1 "2006-01-10T15:39:30Z")

</div>

Sorry for the resend but I am not sure if the first one ever made the  
list.

As mentioned in my previous posts I’m trying to create a dynamic  
directory for file\_folumn. After finding out that I didn’t have the  
most recent version I installed the latest trunk and am able to get  
file\_column to act as it should. However I have been racking my brain  
on how to implement finding my current user from a model. I have  
tried everything in my basic/getting better knowledge of Rails and  
Ruby but have been striking out so far no luck. I am assuming others  
have done this so just some tips on how to get at this info would be  
greatly appreciated. The model that I am trying to get at the user  
info from does have a belongs\_to relationship to my user. Here is my  
last attempt which comes back with a user can’t be found without an  
ID=0 error:

File.join(“test”,User.find(self.user\_id).companyname,“image”)

I have also tried:

File.join(“test”,User.find(self.user.id).companyname,“image”)

And receive the Called id for nil, which would mistakenly be 4 – if  
you really wanted the id of nil, use object\_id error message. By my  
knowledge this makes sense as the object that I am creating has not  
totally been saved yet but I thought that by calling an Object.new  
(params[:object]) that the object at least resides in memory at this  
point (I am doing the object.new at the beginning of my create method  
in the controller for that model). Am I on the right path here or  
just running around in circles?

Thanks for all the help from everyone on the list without it I would  
be having a much harder time than I am getting my head wrapped around  
Ruby and Rails (I am working under a deadline and have been forced to  
learn at the same time that I am writing a product for release for  
our company)

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 10, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/getting-a-user-from-a-model-sort-of-a-noob-resend-sorry/94916/2 "2006-01-10T16:07:38Z")

</div>

On 1/10/06, Andrew F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ruby but have been striking out so far no luck. I am assuming others  
> File.join(“test”,User.find(self.user.id).companyname,“image”)  
> Thanks for all the help from everyone on the list without it I would  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

If your code is in an instance method, then the method will have  
available  
the current record, so you can just reference self.companyname. If your  
code is in a class method, (def self.methodname), then you should be  
passing  
the target user\_id as a parameter.

Is your situation covered by one of these two cases?

–

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 10, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/getting-a-user-from-a-model-sort-of-a-noob-resend-sorry/94916/3 "2006-01-10T16:32:01Z")

</div>

my method is in an episode model which is a different model than my  
user. (two models). file\_column requires this method to be an  
instance method of the model that the file belongs to, and you can’t  
pass any parameters to the method to create the directory when  
calling it from file\_column unfortunately. So companyname is in the  
users table while the file is being saved and created from the  
episodes\_controller and episode model which is set as a belongs\_to  
user relationship.

Hope this explains a little better what is going on.

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 10, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/getting-a-user-from-a-model-sort-of-a-noob-resend-sorry/94916/4 "2006-01-10T18:32:56Z")

</div>

On 1/10/06, Andrew F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Andrew
> 
> > directory for file\_folumn. After finding out that I didn’t have the
> > 
> > (params[:object]) that the object at least resides in memory at this  
> > Andrew  
> > code is in a class method, (def self.methodname), then you should be
> 
> * * *

So, it looks like you’re getting a null reference out of your episodes  
record on ‘user\_id’. Depending on your database, you need to be  
checking  
for same spelling, casing.  
Breakpointer and Irb are great tools for you at this point.

–
