Has_many brings back 'uninitialized constant'


#1

hello. first things first - I am new to ruby on rails (ruby, too), so
I’m figuring this is just a basic newbie error.

I’m creating a basic photography website. And I have two tables with a
relationship:

photos:

  • id (set up as PK)
  • title
  • shutter
  • aperture
  • date
  • the_order
    …etc

photo_comments:

  • id (set up as a PK)
  • photos_id (set up as a FK)
  • author
  • date
  • comment
    …etc

In my photo.rb model file i have this:
class Photo < ActiveRecord::Base
has_many :photo_comment

other code where I use ‘find’ to get the photo info

end

This is all I have in my photo_comment.rb model file:
class Photo_comment < ActiveRecord::Base
belongs_to :Photo
end

From here I’ll admit I’m a little lost as to how to procede. Right now
I’m just wanting to view the comments (i’ll worry about adding comments
next… but one step at a time).

In my photo_controller.rb file I get the desired photo like this:
@photos = Photo.get_photo(params[:photo])

On the view side for this, I have this line to display the photo:

To get the comments, I’ve been trying to do something like this:

<% for a_comment in @photos.photo_comment %>

<%= a_comment.comment %>


<% end %>

However, when I run this code, I get this error:
“uninitialized constant PhotoComment”

Any suggestions on how to fix this, or am I approaching this the wrong
way and need to do it differently?

Thanks in advance, I appreciate it.
-tyler


#2

Hi –

On Mon, 20 Mar 2006, tyler wrote:

This is all I have in my photo_comment.rb model file:
class Photo_comment < ActiveRecord::Base
belongs_to :Photo

Just scanning quickly: change that to:

class PhotoComment < ActiveRecord::Base
belongs_to :photo

and see if that helps :slight_smile:

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#3

unknown wrote:

Hi –

On Mon, 20 Mar 2006, tyler wrote:

This is all I have in my photo_comment.rb model file:
class Photo_comment < ActiveRecord::Base
belongs_to :Photo

Just scanning quickly: change that to:

class PhotoComment < ActiveRecord::Base
belongs_to :photo

and see if that helps :slight_smile:

David

thanks for the reply. I changed it, but I still get the same error.


#4

unknown wrote:

Hi –

On Mon, 20 Mar 2006, tyler wrote:

class Photo < ActiveRecord::Base
has_many :photo_comment

Make that plural (:photo_comments). Another scattershot comment…
sorry I’m being so fragmentary… but maybe that will straighten it
out.

David

I gave it a try, and I then go this error:
“undefined method `photo_comment’”


#5

Hi –

On Mon, 20 Mar 2006, tyler wrote:

class Photo < ActiveRecord::Base
has_many :photo_comment

Make that plural (:photo_comments). Another scattershot comment…
sorry I’m being so fragmentary… but maybe that will straighten it
out.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#6

unknown wrote:

The method will be “photo_comments”.

Thanks again for the response, but I still get this error:

“uninitialized constant PhotoComment”


#7

Hi –

On Mon, 20 Mar 2006, tyler wrote:

out.

David

I gave it a try, and I then go this error:
“undefined method `photo_comment’”

The method will be “photo_comments”.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#8

Hi –

On Tue, 21 Mar 2006, tyler wrote:

unknown wrote:

The method will be “photo_comments”.

Thanks again for the response, but I still get this error:

“uninitialized constant PhotoComment”

I fear I’m out of ideas, based on what you’ve posted. Where (i.e., in
what file) does the error seem to happen?

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#9

would always help if i included the url, ehh?

http://defocused.com/app.zip


#10

David wrote:

I fear I’m out of ideas, based on what you’ve posted. Where (i.e., in
what file) does the error seem to happen?

It happens in my photo view.

I’ve zipped up my app folder, thinking it would probably be easier for
you to take a quick look at that rather than me trying to describe each
file in a post.

I will do a quick explanation of what I zipped though:

I have two models-> photo.rb and photo_comment.rb, along with
controllers to each of these.

I have a view for the photo called ‘get_info.rhtml’ This is the file
that is throwing the error. I have it displaying the info for the photo
correctly. It’s when I try to get comments on the photo that the error
occurs.

I’m referencing these two tables:
photos:

  • id (set up as PK)
  • title
  • shutter
  • aperture
  • date
  • the_order
    …etc

photo_comments:

  • id (set up as a PK)
  • photos_id (set up as a FK)
  • author
  • date
  • comment
    …etc

Thank you so much for helping me with this, I really appreciate it.


#11

David wrote:

and I still say that :slight_smile: You’ve still got Photo_comment.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black

i’m just shaking my head and feeling like a fool. I swear I did that…
but must of done something else… as it’s working fine now.

thank you so much, hope I didn’t waste too much of your time with this
:slight_smile:


#12

Hi –

On Tue, 21 Mar 2006, tyler wrote:

would always help if i included the url, ehh?

http://defocused.com/app.zip

In my first response I said:

and see if that helps :slight_smile:

and I still say that :slight_smile: You’ve still got Photo_comment.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black