Blog Screencast Questions

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.

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)'? 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?

Thanks for any input.

          ... doug

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.

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.

comments is pluralized because a post has many comments (1:n association)

post is singular because a comment belongs to a single post, not several.

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)'? 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?

underscored: blog_comments; blog_post

Fred

Thanks for the input, Rick and Fred.

comments is pluralized because a post has many comments (1:n association)

post is singular because a comment belongs to a single post, not several.

OK. From a grammatical perspective that certainly makes a lot of sense. However, it sure messes with my mind from a programming perspective. Personally, I'd much prefer to be able to refer to something pretty much as I defined it and not have to worry about what is going to be altered and what isn't. I understand that with Ruby there is going to have to be some flexibility in that regard because of Ruby naming rules such as, "constants must begin with a capital letter".

Please correct me if I'm wrong; but, this is an ActiveRecord issue, not a pure Rails issue, right?

Is there anyway that I can turn off the pluralization behavior? That would go a long way towards retaining order in my life.

As far as the camelcase and underscore thing goes, how about I just define my model classes to be 'Blog_post' and 'Blog_comment' respectively? Then, with the pluralization turned off (if that's possible), the only variation that I would have to worry about relates to the case of the initial letter. Dealing with that should be pretty easy because it just requires that one follow the Ruby naming rules. What do you think?

         ... doug

Thanks for the input, Rick and Fred.

> comments is pluralized because a post has many comments (1:n > association)

> post is singular because a comment belongs to a single post, not > several.

OK. From a grammatical perspective that certainly makes a lot of sense. However, it sure messes with my mind from a programming perspective. Personally, I'd much prefer to be able to refer to something pretty much as I defined it and not have to worry about what is going to be altered and what isn't. I understand that with Ruby there is going to have to be some flexibility in that regard because of Ruby naming rules such as, "constants must begin with a capital letter".

Please correct me if I'm wrong; but, this is an ActiveRecord issue, not a pure Rails issue, right?

It's a choice made by the people who wrote active record.

Is there anyway that I can turn off the pluralization behavior? That would go a long way towards retaining order in my life.

Definitely no on/off switch you can through. I suppose you could try messing with inflector rules to make singularising/pluralising a no-op

As far as the camelcase and underscore thing goes, how about I just define my model classes to be 'Blog_post' and 'Blog_comment' respectively? Then, with the pluralization turned off (if that's possible),

Class names should be camel cased: BlogPost. Funny things might happened if you called them Blog_post.

Fred

Class names should be camel cased: BlogPost. Funny things might happened if you called them Blog_post.

OK. So, it sounds like I'm stuck to remember and apply the rules. I'll probably get used to it -- someday. :slight_smile:

Thanks for the input.

         ... doug

Think in English, not programmerspeak, and it will all be effortless. (Thinking in English will probably also lead to writing more readable code in general...)

I totally understand the advantages of this approach. My concern is that when something goes wrong it could compound the difficulty of locating the problem. It's difficult enough trying to locate typos in variable names in interpreted languages without having the language introduce its own variations on spelling. But, the whole thing is neither here nor there. That's the way it is. There is no way to disable that "feature"; and so, I have to live with it. Thanks for the input.

           ... doug

Doug Jolley wrote:

My concern is that when something goes wrong it could compound the difficulty of locating the problem.

I've never had this be an issue at all!

It's difficult enough trying to locate typos in variable names in interpreted languages

(Nitpick: this has nothing to do with Ruby being interpreted. Interpreted languages can require variable declarations, while compiled ones need not necessarily.)

without having the language introduce its own variations on spelling.

Actually, it's the framework doing that...but again, the whole thing is pretty transparent if you speak English and know Ruby's capitalization rules.

But, the whole thing is neither here nor there. That's the way it is. There is no way to disable that "feature"; and so, I have to live with it.

Perhaps you shouldn't be using Rails if this really bothers you. Rails is a great framework, but it does a great deal of magic -- almost too much. There are other Ruby Web frameworks such as Merb and Ramaze that may be more to your taste.

Personally, I like the Rails inflections. They work by well-defined rules, and knowing what's what has never been a problem for me in practice.

Thanks for the input.

           ... doug

You're welcome!

Best,

Actually, it's the framework doing that.

Actually, if I am not mistaken, it's not the framework (Rails). It's ActiveRecord. I certainly wouldn't want to give up ActiveRecord. That would be asking too much! :slight_smile:

Perhaps you shouldn't be using Rails if this really bothers you.

Well, I must admit that it does bother me. However, maybe I'm just too set in my ways. Maybe I'll get used to it after a while. Anyway, I'm going to give it a fair try before I through in the towel. There are too many other things about Rails that I really like.

As one commentator put it:

Sit back, relax, and prepare to be assimilated...

That's exactly what I plan on doing. Besides that, his point about sharing code is well taken.

Thanks again for all the input.

           ... doug