# Has\_many problem //still new at this (somewhat)

**URL:** <https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471>\
**Category:** Rails\
**Created:** [January 29, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471 "2008-01-29T16:54:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 29, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/1 "2008-01-29T16:54:03Z")

</div>

So, I’m having problems displaying many images connected to a specific  
user (current\_user [restful authentication]) when visiting the current  
user’s “/portfolio”.

This is what I thought it should look like:

> > > > > > > > > > > > > > > > > > > > >

Entry model:

> > > > > > > > > > > > > > > > > > > > >

```
file_column :image
belongs_to :user

```

> > > > > > > > > > > > > > > > > > > > >

User model:

> > > > > > > > > > > > > > > > > > > > >

has\_many :entries, :dependent =\> true

> > > > > > > > > > > > > > > > > > > > >

EntryController

> > > > > > > > > > > > > > > > > > > > >

def portfolio  
end

> > > > > > > > > > > > > > > > > > > > >

portfolio.rhtml

> > > > > > > > > > > > > > > > > > > > >

\<% @current\_user.entries.each do |entry| %\>  
\<%= image\_tag url\_for\_file\_column( “entry”, “image”) %\>  
\<% end %\>

\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<  
what I recieve is the following message:

“You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.entries”

\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<  
NOTE:  
\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<  
The table: Entries  
Columns: id, image, user\_id  
(no problem here)

> > > > > > > > > > > > > > > > > > > >

Well, something is wrong and it might be very simple for you…but I’ve  
been struggling with this for two days now and have run out of ideas.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 29, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/2 "2008-01-29T17:34:29Z")

</div>

Dag S. wrote:

> \<% @current\_user.entries.each do |entry| %\>  
> \<%= image\_tag url\_for\_file\_column( “entry”, “image”) %\>  
> \<% end %\>

Dag,

I’m having trouble finding the documentation for url\_for\_file\_column, so  
this is more of a shot in the dark then anything else, but I think the  
problem lies in using the string “entry” rather then the local variable  
entry in that method call.

You have gone through each image the user has and the record is assigned  
to the local variable entry in the block, but no where in that block do  
you make reference to that local variable, instead what I see is the  
quoted string “entry.”

The function url\_for\_file\_column looks like it is a helper function  
perhaps provided by a plugin? My first try would be to track down the  
documentation or source code for that function and check that you are  
giving it appropriate values. Another thought is that somewhere there  
must be a controller that is responding to the image request. (we know  
this because a controller/model is the only way to get something out of  
the database, and you said that is where the images are stored)

If this doesn’t help, then you will need to post more of the error  
message so someone can see just exactly where that unexpected nil is in  
the code.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 29, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/3 "2008-01-29T18:24:00Z")

</div>

> Another thought is that somewhere there  
> must be a controller that is responding to the image request. (we know  
> this because a controller/model is the only way to get something out of  
> the database, and you said that is where the images are stored)

hi, Starr T.

well, what I meant (and I was unclear) is that a referance name like  
“test.jpeg” is stored there, and the actual file is stored in  
“\public\entry\image\2”  
where “2” matches the “id”.

There is a model responding to the image request, but no controller (the  
controller only directs to the profile view (but is used to upload a  
picture).

Yes, this is part of a plugin called “file\_column”:  
[http://wiki.rubyonrails.org/rails/pages/HowToUseFileColumn](http://wiki.rubyonrails.org/rails/pages/HowToUseFileColumn)  
[http://webonrails.wordpress.com/2006/06/22/plugin-file\_column/](http://webonrails.wordpress.com/2006/06/22/plugin-file_column/)  
[http://www.kanthak.net/opensource/file\_column/#download](http://www.kanthak.net/opensource/file_column/#download)

And the function “url\_for\_file\_column” is a helper-function, like you  
assumed.

The thing is that all of this works fine, with no problem at all, as  
long as I don’t try “converting” it to “has\_many” (and I thought that  
would be an easy nut to crack). Silly me. There is not very much  
documentation at all, but it was easy to implement and had got good  
reviews so that’s why.

To make it clearer how it works for ONE image in the view (I made a  
backup, luckily)

As you’ve seen before (my version with many):

> > \<% @current\_user.entries.each do |entry| %\>  
> > \<%= image\_tag url\_for\_file\_column( “entry”, “image”) %\>  
> > \<% end %\>

What you said: “but I think the problem lies in using the string “entry”  
rather then the local variable entry in that method call.”

> You have gone through each image the user has and the record is assigned  
> to the local variable entry in the block, but no where in that block do  
> you make reference to that local variable, instead what I see is the  
> quoted string “entry.”

basically what I understood was that I didn’t use a referance to the  
local variable entry.

and in the original (which worked) they make a referance “@entry”:

```
<% @entry = current_user.entry(params[:id]) %>
<%= image_tag url_for_file_column("entry", "image") %>

```

(but using this with “entries” instead “entry”… well it leaves another  
error:  
“You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.image\_relative\_path”)

thanks for your input, hopefully this will make it easier for you to  
understand.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 30, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/4 "2008-01-30T13:49:50Z")

</div>

The user (user\_id 47) has two pictures in the database table Entry.

“NoMethodError in Entry#portfolio  
Showing app/views/entry/portfolio.rhtml where line #13 raised:  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.image\_relative\_path”

C:/ruby/authentication/vendor/plugins/trunk/lib/file\_column\_helper.rb:75:in  
`send' C:/ruby/authentication/vendor/plugins/trunk/lib/file_column_helper.rb:75:in`url\_for\_file\_column’  
C:/ruby/authentication/app/views/entry/portfolio.rhtml:13:in  
`_run_rhtml_47app47views47entry47portfolio46rhtml' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/associations/association_proxy.rb:123:in`each’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/associations/association\_proxy.rb:123:in  
`send' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/associations/association_proxy.rb:123:in`method\_missing’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/associations/has\_many\_association.rb:98:in  
`method_missing' C:/ruby/authentication/app/views/entry/portfolio.rhtml:11:in`\_run\_rhtml\_47app47views47entry47portfolio46rhtml’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_view/base.rb:325:in  
`send’

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 30, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/5 "2008-01-30T15:04:39Z")

</div>

okay, moving on with more info. I found this in the helper-file

relative\_path = object.send("#{method}\_relative\_path", subdir)  
return nil unless relative\_path

I’ve made an attachment of it.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 29, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/6 "2008-01-29T18:27:43Z")

</div>

I’ve tried to apply these solutions with no success. A person with the  
same problem like me, in the next thread there comes a solution, but it  
doesn’t really work for me.

[http://readlist.com/lists/lists.rubyonrails.org/rails/0/4600.html](http://readlist.com/lists/lists.rubyonrails.org/rails/0/4600.html)

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 2, 2008, 8:31am UTC](https://www.ruby-forum.com/t/has-many-problem-still-new-at-this-somewhat/127471/7 "2008-02-02T08:31:05Z")

</div>

Dag S. wrote:

> okay, moving on with more info. I found this in the helper-file
> 
> relative\_path = object.send("#{method}\_relative\_path", subdir)  
> return nil unless relative\_path
> 
> I’ve made an attachment of it.

Sorry I took so long, Life happened. Good news on all fronts though.  
The file gives the answer to the problem:

def url\_for\_file\_column(object, method, options=nil)  
case object  
when String, Symbol  
object = instance\_variable\_get("@#{object.to\_s}")  
end  
# …  
end

What this says is that when you pass in a String or a Symbol, the helper  
expects an instance variable (with an @ in front) of that same name to  
exist. In your case it is a local variable because it is part of the  
block. The fix: remove the quotes from around entry. This will send it  
the object you want.

\<%= image\_tag url\_for\_file\_column( entry, “image”) %\>

If you want my opinion, the use of this meta programming hack was a poor  
choice by the authors as you can always pass @entry directly and save  
and expensive table lookup, but what do I know.
