Hello, At risk of being named a Troll after the rather heated discussion of earlier today, I''d like to better understand the thinking behind the design decision to pluralise things. Please note that I am a newbie, and to this newbie, pluralisation is quite confusing. I''m going to start from the assumption that pluralisation is a really good idea and like so many things in Rails, I simply don''t yet understand its subtlety and power. I''m hoping someone can give me a deeper understanding of the magic of pluralisation. On the surface pluralisation seems to add complexity and rules to Rails, which generally appears to be a framework designed to reduce complexity and rules. Strange. One of the most confusing things for the Rails beginner (i.e. me) is constantly asking myself questions like "Why is this pluralised?", "Am I meant to be using singular or plural?", "Is this problem caused by the fact that something should be plural, or something that should be singular?">From an intutive perspective, I would much prefer that Rails did not doanything that I did not ask it to do, like pluralisation. In any other development environment I know if I name a database table "blahwidget", then for the rest of the lifetime of the application I can reasonably assume that I will be referring to the blahwidget table. Under Rails however, it seems that it may or may not be necessary to refer to it as blahwidgets, or maybe the table must be called blahwidgets rather than blahwidget in the first place. As you can see, it''s confusing to the uninitiated. It seems very important to me to be able to assume that an entire system deals with the blahwidget as-is. No modifications, no references to "blahwidgets". So I guess the question of this email is; "what is it that pluralisation adds that is so powerful that it justifies adding in the significant confusion of trying to work out how pluralisation fits into the entire Rails picture?" Programming languages and frameworks are complex enough without bringing in the even more complex elements of the English language like pluralisation. Could someone please set me straight? I''d like to hear that it is much easier to understand than I think, and that it adds enormous value to Rails. And no I''m no trolling. I just spent three days of a long weekend trying to get Rails to work - I''m investing in learning this thing. Cheers Andrew Stuart
I''m new to Rails also, and I always called my tables in the singular. But I prefer the rails way now. I think of tableS as containerS of many widgetS, and relationships between models as encompassing one widget or many widgetS. After a little use, it becomes second nature, and my old singular naming technique seems flawed. Cheers, Neville -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Andrew Stuart Sent: Tuesday, 15 March 2005 5:09 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] What''s the thinking behind pluralisation? Hello, At risk of being named a Troll after the rather heated discussion of earlier today, I''d like to better understand the thinking behind the design decision to pluralise things. Please note that I am a newbie, and to this newbie, pluralisation is quite confusing. I''m going to start from the assumption that pluralisation is a really good idea and like so many things in Rails, I simply don''t yet understand its subtlety and power. I''m hoping someone can give me a deeper understanding of the magic of pluralisation. On the surface pluralisation seems to add complexity and rules to Rails, which generally appears to be a framework designed to reduce complexity and rules. Strange. One of the most confusing things for the Rails beginner (i.e. me) is constantly asking myself questions like "Why is this pluralised?", "Am I meant to be using singular or plural?", "Is this problem caused by the fact that something should be plural, or something that should be singular?">From an intutive perspective, I would much prefer that Rails did not doanything that I did not ask it to do, like pluralisation. In any other development environment I know if I name a database table "blahwidget", then for the rest of the lifetime of the application I can reasonably assume that I will be referring to the blahwidget table. Under Rails however, it seems that it may or may not be necessary to refer to it as blahwidgets, or maybe the table must be called blahwidgets rather than blahwidget in the first place. As you can see, it''s confusing to the uninitiated. It seems very important to me to be able to assume that an entire system deals with the blahwidget as-is. No modifications, no references to "blahwidgets". So I guess the question of this email is; "what is it that pluralisation adds that is so powerful that it justifies adding in the significant confusion of trying to work out how pluralisation fits into the entire Rails picture?" Programming languages and frameworks are complex enough without bringing in the even more complex elements of the English language like pluralisation. Could someone please set me straight? I''d like to hear that it is much easier to understand than I think, and that it adds enormous value to Rails. And no I''m no trolling. I just spent three days of a long weekend trying to get Rails to work - I''m investing in learning this thing. Cheers Andrew Stuart _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, 15 Mar 2005 17:09:18 +1100, Andrew Stuart <andrew.stuart-HYVr4Vs6GMsQrrorzV6ljw@public.gmane.org> wrote:> In any other development environment I know if I name a database table > "blahwidget", then for the rest of the lifetime of the application I can > reasonably assume that I will be referring to the blahwidget table. Under > Rails however, it seems that it may or may not be necessary to refer to it > as blahwidgets, or maybe the table must be called blahwidgets rather than > blahwidget in the first place. As you can see, it''s confusing to the > uninitiated. It seems very important to me to be able to assume that an > entire system deals with the blahwidget as-is. No modifications, no > references to "blahwidgets".I think the idea of pluralisation is that it makes your code easier to read as plain english. And it''s easy to tell whether to pluralise the word or not, just think of the normal English rules (yes, I''m aware of how ironic that is... ;) You''d want to call your table blahwidgets, pluralised, since your table will presumably have more than one row in it. Each row is a single blahwidget, the table contains all the blahwidgets. So your model will be named in the singular (blahwidget.rb), because the model is one singular blahwidget that represents the structure of each blahwidget individually. In turn, your controller will be named in the plural, since it is representing how all of the blahwidgets interact with each other and the user (though each action will be different; eg, your show action will create one blahwidget in the @blahwidget variable, but your list action will create an array of blahwidgets in @blahwidgets, which you then iterate over for the purpose of displaying. Just think, from the perspective of an English speaker, how awkward these phrases are: has_many :blahwidget for articles in categories.article These statements are nonsensical. In the first line, you have many of just one thing, and in the second line, you are taking plural articles from just one thing (the second line is really bad, it makes it look like you''re taking one article from several categories, and then taking several articles from just the one article. hooray for contrived examples!). So rails introduces nice pluralisation rules to make the code more pretty: has_many :blahwidgets for article in category.articles It makes much more sense grammatically. You can have one blahwidget, or many blahwidgets. In the second line, you are looking at the current category, getting the articles from it, and iterating over each article. I''m not aware of any really profound benefits to this, other than "it makes the code easier to read". I find it totally natural. If you''re struggling to figure out when to pluralise and when not, all I can really think is, is english not your first language? -- One Guy With A Camera http://rbpark.ath.cx
John-Mason P. Shackelford
2005-Mar-15 18:36 UTC
Re: What''s the thinking behind pluralisation?
Andrew, I found when training another developer to use rails that this was often confusing to him. It threw me at first too since just about every book on database theory I''ve read uses singular entity names--a standard among popular ER standards like Martin for instance. Most formal treatments I''ve seen are agreed on this point and I don''t think its even up for discussion in academic circles, but in practice there appears to be a diversity of opinion. I''d hate to be responsible for kicking off a fiery debate on the topic but, Andrew, you are not alone in thinking that this feature (which strikes as me pretty neat, though perhaps excessive) is also confusing at first. I am no troll either, hit 3170 LOC today. :) John-Mason P. Shackelford
Andrew Stuart
2005-Mar-16 00:01 UTC
Newbie question - Best Practice for adding fields to Rails application
Hello, This is a newbie question. I''ve scanned the documentation for a HOWTO but can''t seem to see a relevant one. I have a small database (actually the todo database from the tutorial). All seems to be working in that Rails works and I can view, add, edit, remove records. I would now like to start adding fields to the application and extending it iteratively. I assume though that adding fields to the database will make it necessary for me to regenerate all the code by running the generate scripts again including generate scaffold. Is this the correct way to add fields - i.e. to add fields to database then regenerate all code? What happens to any customisations that I might have made to the generated code, for example new.rhtml? Thanks in advance. Andrew Stuart
John Wilger
2005-Mar-16 04:10 UTC
Re: Newbie question - Best Practice for adding fields to Rails application
On Wed, 16 Mar 2005 11:01:55 +1100, Andrew Stuart <andrew.stuart-HYVr4Vs6GMsQrrorzV6ljw@public.gmane.org> wrote:> I assume though that adding fields to the database will make it necessary > for me to regenerate all the code by running the generate scripts again > including generate scaffold. Is this the correct way to add fields - i.e.No, you definitely should _not_ regenerate. Actually, one of ActiveRecord''s best features is the fact that, once you add the column to your table, the property is automatically available to the model class. This is because AR uses run-time reflection on the database to determine what attributes to present. -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
Rob Park
2005-Mar-16 07:08 UTC
Re: Newbie question - Best Practice for adding fields to Rails application
On Wed, 16 Mar 2005 11:01:55 +1100, Andrew Stuart <andrew.stuart-HYVr4Vs6GMsQrrorzV6ljw@public.gmane.org> wrote:> I assume though that adding fields to the database will make it necessary > for me to regenerate all the code by running the generate scripts again > including generate scaffold. Is this the correct way to add fields - i.e.No, this is definitely the _worst_ thing you could do, unless you enjoy starting your application from scratch every time you add a field to the database. If you add a field, your models will automatically pick them up and know about them. The only things you''ll have to do are: - add a bit in your views to be able to display and edit this new field - add the validations for this field into the model And you shouldn''t even have to do anything to your controllers.> to add fields to database then regenerate all code? What happens to any > customisations that I might have made to the generated code, for example > new.rhtml?You lose it. End of story. -- One Guy With A Camera http://rbpark.ath.cx
John-Mason P. Shackelford wrote:> I found when training another developer to use rails that this was often > confusing to him. It threw me at first too since just about every book > on database theory I''ve read uses singular entity names--a standard > among popular ER standards like Martin for instance. Most formal > treatments I''ve seen are agreed on this point and I don''t think its > even up for discussion in academic circles, but in practice there > appears to be a diversity of opinion. I''d hate to be responsible for > kicking off a fiery debate on the topic but, Andrew, you are not alone > in thinking that this feature (which strikes as me pretty neat, though > perhaps excessive) is also confusing at first.Some of the clients I work for use singular, some plural. The books I have on databases and SQL are divided (interestingly, the Oracle 8i Certification Course uses singular, but the sample schemas for Oracle 9i use plural). I don''t think Rails is quite there yet in being able to deal with legacy databases, but even in "green field" situations you have to follow the data modelling standards of your clients. So why not make it an option? Justin
David Heinemeier Hansson
2005-Mar-16 19:13 UTC
Re: What''s the thinking behind pluralisation?
> I don''t think Rails is quite there yet in being able to deal with > legacy databases, but even in "green field" situations you have to > follow the data modelling standards of your clients. So why not make > it an option?It is. And has been almost since the beginning: ActiveRecord::Base.pluralize_table_names = false -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://www.loudthinking.com/ -- Broadcasting Brain
David Heinemeier Hansson wrote:>> I don''t think Rails is quite there yet in being able to deal with >> legacy databases, but even in "green field" situations you have to >> follow the data modelling standards of your clients. So why not make >> it an option? > > > It is. And has been almost since the beginning: > > ActiveRecord::Base.pluralize_table_names = falseMany thanks. Justin
Pluralisation(?) doesn''t bother me because that''s the way I code anyway. One thing that was a bit worrying when starting with Rails was a little inconsistency I found. All the tutorials I read used the generator controller to create a controller with a singular name. eg TodoController, but when I used the scaffold generator it created plural controllers. This freaked me out a bit because of a statement I read where Rails uses the names to find the correct classes to use. It all seems to work though so maybe not. Is this a generator bug or out of date docs or "It just doesn''t matter"? Henry
On Thu, 17 Mar 2005 17:16:15 +1300, Henry Maddocks <henryj-wUU9E3n5/m4qAMOr+u8IRA@public.gmane.org> wrote:> All the tutorials I read used the generator controller to create a > controller with a singular name. eg TodoController, but when I used the > scaffold generator it created plural controllers. This freaked me out a > bit because of a statement I read where Rails uses the names to find > the correct classes to use. It all seems to work though so maybe not. > > Is this a generator bug or out of date docs or "It just doesn''t matter"?I noticed that issue, too. I think what''s happened is that when you actually called the "script/generate controller", you gave it a singular name, and it just uses whatever name you say. but when you generate the scaffolding, it generates the models and the controllers and all that for you, so it has to diddle with the pluralisation rules for you anyway, and it assumes you want plural controllers. Personally I think pluralised controllers make more sense anyway. "photo/list" doesn''t make any sense, you''re listing one photo? No, it''s a list of photoS, so you want "photos/list". For the show action, it could go either way, really. "photo/show/1" would be "show photo #1", which makes sense, but then "photos/show/1" would be "of all the photos, show #1", which also makes sense. -- One Guy With A Camera http://rbpark.ath.cx
Rob Park wrote:> On Thu, 17 Mar 2005 17:16:15 +1300, Henry Maddocks > <henryj-wUU9E3n5/m4qAMOr+u8IRA@public.gmane.org> wrote: > >>All the tutorials I read used the generator controller to create a >>controller with a singular name. eg TodoController, but when I used the >>scaffold generator it created plural controllers. This freaked me out a >>bit because of a statement I read where Rails uses the names to find >>the correct classes to use. It all seems to work though so maybe not. >> >>Is this a generator bug or out of date docs or "It just doesn''t matter"? > > > I noticed that issue, too. I think what''s happened is that when you > actually called the "script/generate controller", you gave it a > singular name, and it just uses whatever name you say. but when you > generate the scaffolding, it generates the models and the controllers > and all that for you, so it has to diddle with the pluralisation rules > for you anyway, and it assumes you want plural controllers. > > Personally I think pluralised controllers make more sense anyway. > "photo/list" doesn''t make any sense, you''re listing one photo? No, > it''s a list of photoS, so you want "photos/list". For the show action, > it could go either way, really. "photo/show/1" would be "show photo > #1", which makes sense, but then "photos/show/1" would be "of all the > photos, show #1", which also makes sense. >I also noticed this. But, by your logic wouldn''t we also want pluralized models? I mean, when you say Photo.find_all, you don''t want to find all of one photo, right? Maybe people should just stick to always using pluralized forms for everything in the MVC. PS- I am a Noob so I might be way off base. -- Tabor Kelly tkelly-rails-15BJK4dlZCtAtc6zTKlAYNHuzzzSOjJt@public.gmane.org http://tabor.taborandtashell.net
Models are always singular (unless you are modelling a list). Collections of models are plural and find_all returns a collection. Controllers could be either and we can argue this until the cows come home because it doesn''t really matter unless Rails is doing reflection to figure out what calls to make which was my main concern. Plus because it didn''t match the tutorials. It seems to be working so I guess it doesn''t matter. On 17/03/2005, at 8:00 PM, Tabor Kelly wrote:> I also noticed this. But, by your logic wouldn''t we also want > pluralized models? I mean, when you say Photo.find_all, you don''t want > to find all of one photo, right? Maybe people should just stick to > always using pluralized forms for everything in the MVC.