# Executing a model from two different controllers - one contr

**URL:** <https://www.ruby-forum.com/t/executing-a-model-from-two-different-controllers-one-contr/110059>\
**Category:** Rails\
**Created:** [July 22, 2007, 8:03am UTC](https://www.ruby-forum.com/t/executing-a-model-from-two-different-controllers-one-contr/110059 "2007-07-22T08:03:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aquaflow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@aquaflow](https://www.ruby-forum.com/u/aquaflow)\
**Post date:** [July 22, 2007, 8:03am UTC](https://www.ruby-forum.com/t/executing-a-model-from-two-different-controllers-one-contr/110059/1 "2007-07-22T08:03:48Z")

</div>

My appllication uses a “Video” table. I have a video\_controller and  
associated video model

The video\_controller requirs the user to login to the system.  
class VideoController \< ApplicationController  
before\_filter :login\_required

When I initially wrote code for the Video model, I assumed that only  
authenticated users would read, write Video records. Hence, I’ve got  
code in the model such as:  
@dir = ‘public/uploaded\_videos’ + “/” + User.current\_user.id

The above works fine when a logged in user accesses video functions on  
the site.

Now, I want to display a list of videos on the Index/home page of the  
web application. Users who access the web page don’t need to log in to  
the system.

I created a home\_controller with an index method.  
def index  
@videos = Video.find(:all, :conditions =\> [“public = 0”], :select =\>  
[‘id, file\_name, file\_size,thumbnail’])

end

Whenever, this method is executed, RoR makes a call to the video.rb  
file and fails on the following line:  
@dir = ‘public/uploaded\_videos’ + “/” + User.current\_user.id

It’s failing because User.current\_user.id is null. This value is only  
populated when a user logs into the system.

How can I get around this particular issue?

---

<div class="post-metadata">

**Author:** ![aquaflow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@aquaflow](https://www.ruby-forum.com/u/aquaflow)\
**Post date:** [July 22, 2007, 9:44am UTC](https://www.ruby-forum.com/t/executing-a-model-from-two-different-controllers-one-contr/110059/2 "2007-07-22T09:44:37Z")

</div>

not that it makes any difference (you’ll still solve the problem  
regardless, but for general knowledge, where is the @dir statement being  
called … ?

either way, just add a ‘unless User.current\_user.nil?’

> ```
> @dir = 'public/uploaded_videos' + "/" + User.current_user.id
> 
> ```

@dir = 'pub … + User.current\_user.id unless User.current\_user.nil?

should solve the problem, no?

---

<div class="post-metadata">

**Author:** ![aquaflow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@aquaflow](https://www.ruby-forum.com/u/aquaflow)\
**Post date:** [July 22, 2007, 10:28am UTC](https://www.ruby-forum.com/t/executing-a-model-from-two-different-controllers-one-contr/110059/3 "2007-07-22T10:28:33Z")

</div>

aquaflow wrote:  
Hence, I’ve got

> code in the model such as:  
> @dir = ‘public/uploaded\_videos’ + “/” + User.current\_user.id

I just want to point out something, the code above should not be in the  
model, but in the helpers.

---

<div class="post-metadata">

**Author:** ![aquaflow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f07891/32.png) [@aquaflow](https://www.ruby-forum.com/u/aquaflow)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/executing-a-model-from-two-different-controllers-one-contr/110059/4 "2007-07-26T00:45:45Z")

</div>

Thank you. That worked perfectly.

I had previsouly tried using an if statement with the same condition:

# if !(User.current\_user.nil?)

# DIRECTORY = ‘public/uploaded\_videos’ + “/” + User.current\_user.id

# end

It seemed like the if statement was being ignored.

@dir is being called from other methods within the model. I’m using it  
ti define the destination file path for encoded video files.

On Jul 22, 12:44 am, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
