Has_many problem //still new at this (somewhat)

Dag Stensson 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.

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 Trader

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://webonrails.wordpress.com/2006/06/22/plugin-file_column/
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.

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

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'

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.

Attachments:
http://www.ruby-forum.com/attachment/1357/file_column_helper.rb

Dag Stensson 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.