Forum: Ruby on Rails Getting a user from a model (sort of a noob) (resend sorry)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 Andrew Filipowski (Guest)
on 2006-01-10 15:39
(Received via mailing list)
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
3203ed0e608d3bfae1e31282e629ffa2?d=identicon&s=25 Peter Fitzgibbons (Guest)
on 2006-01-10 16:07
(Received via mailing list)
On 1/10/06, Andrew Filipowski <a.filipowski@mac.com> 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
> Rails@lists.rubyonrails.org
> 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?

--
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 Andrew Filipowski (Guest)
on 2006-01-10 16:32
(Received via mailing list)
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
3203ed0e608d3bfae1e31282e629ffa2?d=identicon&s=25 Peter Fitzgibbons (Guest)
on 2006-01-10 18:32
(Received via mailing list)
On 1/10/06, Andrew Filipowski <a.filipowski@mac.com> 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.

--
This topic is locked and can not be replied to.