Forum: Ruby on Rails has_many brings back 'uninitialized constant'

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.
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-20 03:29
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:
<img src="/photos/<%= @photos.the_order %>.jpg" />

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

<% for a_comment in @photos.photo_comment %>
	<p><%= a_comment.comment %></p>
<% 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-20 03:56
(Received via mailing list)
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 :-)


David

--
David A. Black (dblack@wobblini.net)
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
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-20 05:46
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 :-)
>
>
> David

thanks for the reply.  I changed it, but I still get the same error.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-20 05:55
(Received via mailing list)
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 (dblack@wobblini.net)
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
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-20 15:20
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'"
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-20 15:23
(Received via mailing list)
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 (dblack@wobblini.net)
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
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-21 00:56
unknown wrote:

>
> The method will be "photo_comments".
>

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

"uninitialized constant PhotoComment"
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-21 01:08
(Received via mailing list)
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 (dblack@wobblini.net)
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
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-21 01:51
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.
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-21 02:39
would always help if i included the url, ehh?

http://defocused.com/app.zip
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-21 03:16
(Received via mailing list)
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 :-)

and I *still* say that :-)  You've still got Photo_comment.


David

--
David A. Black (dblack@wobblini.net)
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
3d530114523d9cd69fca11d4db16ab9f?d=identicon&s=25 tyler (Guest)
on 2006-03-21 03:30
David wrote:

>
> and I *still* say that :-)  You've still got Photo_comment.
>
>
> David
>
> --
> David A. Black (dblack@wobblini.net)
> 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
:-)
This topic is locked and can not be replied to.