On Mon, Jun 15, 2009 at 11:31 AM, doug<[email protected]> wrote:
>
> In the blog screencast the commentator creates two model classes,
> 'Post' and 'Comment'. Note that both of these model classes are
> singular. However, when he creates the association he says in the
> Post model 'has_many(:comments)'. IOW, he uses the plural form of the
> Comment model and lowers the case of the the model class name.
> However, when he does the association within the Comment class he says
> 'belongs_to(:post)'. Here he does not pluralize post although he does
> (as expected) lower the case. This perplexes me.
The rules are pretty obvious once you realize that the goal was to
follow normal English syntax.
I wouldn't say "I have many problem." Neither would I say "I have a problems"
The other Rails convention here is the relationship between Ruby
constant names (and class names and module names are constants) and
the symbols Rails uses to talk about an instance or instances of a
class.
Ruby requires constants to be named with an initial capital letter.
In forming compound words, the convention is to use what is called
"camel-case", as in ActionController, or BlogPost, it's called camel
case because the capital letters form 'humps.'
But other ruby names use lowercase, and most Rubyists use underscores
to combine words in these, as in first_name. Most folks call this
'snake case'.
>
> Now, I want to follow this example; but, I want my model classes to be
> named 'BlogPost' and 'BlogComment' respectively in order to avoid
> potential namespace conflicts. I am not getting my associations to
> work. I think that I may be having trouble with the naming. In the
> BlogPost class do I say, 'has_many(:blogComments)' and in the
> BlogComment class say, 'belongs_to(:blogPost)'?
So following both conventions this should be
class BlogPost < ActiveRecord::Base
has_many :blog_comments
end
class BlogComment < ActiveRecord::Base
belongs_to :blog_post
end
> This pluralizing
> thing is really confusing me. Additionally, I'm beginning to wonder
> if I don't need to introduce some underscores, e.g., 'blog_comment' or
> 'blog_comments'. Anyway, I'm obviously very confused. Can someome
> please straighten me out on exactly how these associations should be
> formed?
I hope that this post helped do just that.
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---