Model name 'Resource' - is it safe? - ruby-on-rails-3

I checked Rails and Ruby restricted keywords, and looks like Resource as a model name should be fine. However I would like to get some confirmation from advanced Rails developer.

I did it a quick google for reserved keywords rails and whilst I found your model name Resource not on any of the lists, I think it is wise not to use it.
The reason for this is that it's in the very nature of programming and frameworks, that these lists tend to change over time.
So what seems like a good naming idea now, might not be such a good idea in the future. For this reason alone I would stray away from using something that looks like it could be a reserved keyword.
Maybe use namespaces?

Related

Naming camelcase function names containing camelcase brand names

There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton
I'm having a function called isIos() which returns true if the device is iOS (this is a cordova app).
iOS is the correct brand syntax but the coding style implmented used camelcase for the function name.
Should the function name be:
isIos() or isiOs() or isIOs() or something else?
What is the recommended way of dealing with this?
There's no general recommendation, your language might have guidelines that you might want to follow (doesn't seem the case with Node.js or javascript), or your company or application might have guidelines that you will have to follow, or there might be existing uses of the term in the application that you'll probably want to imitate, otherwise just use what seems easiest to read.
In this case, it might be isIOS, or isIos.
If you're using camel case though you should probalbly always put in upper case the first character of each word, isiOs() seems very confusing.
Your best option in these cases anyway is often to spare you the embarrassment and use a different name, if you can come up with one.
EDIT:
If you are not required to follow strict camel case you can also settle to is_iOS, which lets you keep the original case. This is probably the best alternative.

Rails's way of 'convention over configuration'; are there pitfalls?

After many years of C/C++, PHP, some Ruby and other languages on one hand an different projects with different frameworks on the other, I now want to learn Rails.
After working through (Getting started-) Guides, I think Rails is powerfull and fairly easy to learn. And I feel ready to start with a non Bookshop app.
But a friend warned me about Rails's 'convention over configuration' and the way it 'does things'. I cant see a 'problem' with that, but are there pitfalls?
And: Are there things Rails does very different than other framworks?
You're going to either get zero replies or a bunch of opinions. I would recommend googling for "rails is opinionated". Hopefully that will turn up more examples of what you might run into.
Is it a problem? No, not really. Can it be a problem? Yes, absolutely.
Integrating with legacy databases can be a PITA sometimes. Or if you have some insane desire to name all your primary keys something other than "id" that can be a problem.
Not so much a problem really, but you're fighting a lot of convention.
Really, other than legacy databases I can't think of anything off the top of my head bothers me about it's conventions.
What your friend says is only somehow true. Rails's naming conventions are powerfull and keep your brain free for other things.
But: If you think you have experience ... you are learning Rails -> you are back to school.
Rail's 'naming conventions' are not only conventions. They are Rails somehow. So if you break the convention, you are off road and soon in the middle of no where. I think that this part of Rails could be better pointed out in Guides.
Let me give an example: (you are tired of "books" and start wit a little app around 'Pubs')
You scaffold your Pup (intended typo)
You then put in some logic, put some work, then you realize your (oops) typo. Now dark clouds arise. Since you are experienced, you start correcting the typo ... PupsController -> PubsController, filename of PubsController (you are already off road) ...
You will end up at the database table 'pups' ... (middle of nowhere)
This happened because you think you are experienced. A beginner has built a new scaffold (without typo) or asked here on SO how to correct 'correctly')
An other example is to name thigs 'more nice'. After years and many projects you are probably one, that never uses "unspecified" names like 'user','role','guest','owner' for classes and so on. So you start to name them (nicley?): PubUser, PubOwner, ... Noboddy told you "DON'T".
You put all in a namespace (there are many people here saying "don't") with the nice name 'PubApp'
Although your files are well organized, you will end in tablenames: pub_app_pub_owners and so on, not to think about the name of assotiative Tables between them.
And later on you will type something like
link_to 'add' new_pub_app_pub_pub_guest_url
link_to 'add' new_pub_app_pub_owner_pub_url
This is probaly not what your intention was to make things 'clean'. And if you take a look at the 'beginers' link...
link_to 'add' new_pub_guest_url
What I do not want is to preferre one or the other.
I want to point out, that - since you are not experienced with Rails - you dont know where the things you are doing (off road) are leading you. With only a hard way to return.
Thats somehow a pitfall.
But next time you will know about that and make a compromise: 'Pubowner' and 'Guest' (and 'pa' as namespace (if you realy want to)
so
link_to 'add' new_pa_pubowner_guest_url
Is not so bad. But its hard to reverse things so think before ...
When writing application using any framework it may be necessary to write a lot of configuration code. However if we follow Rails Standard conventions then it is possible to avoid the excess configuration and in some cases no configuration at all. Thus, explicit configuration would be needed only in those cases where you can't follow the standard convention.
Following are the conventions provided by rails:
Naming conventions: Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books.
Example:
Database Table - Plural with underscores separating words (e.g., book_clubs).
Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).
Schema Conventions: Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.
Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id, order_id). These are the fields that Active Record will look for when you create associations between your models.
Primary keys - By default, Active Record will use an integer column named id as the table's primary key. When using migrations to create your tables, this column will be automatically created.
There is a point I never had before, that took me a nerve or two: Rails is caching a lot - even in development env. (for gods sake it does!)
Let me construct a scenario (no; not a construct, happend to me in a more complex variant)
After enough hours of work and you did all that was on the plan for the day, you close with a little cleanup. Check evering is still working - smile - and off
sleep
Back to the Computer you start all up and get a 'constant xy is not ...', so but, but why, overnight?
The answer is easy (if someone tells you at least once): Rail's caching does not (or better cant) check if a class / file / method is just removed, and not altered (sometimes ...)
So the (one to many) deleted file removed the Class, it contained from the world, but not from Rail's cache. Power off did the rest.
I had more subtile situations, that i solved with a rails server restart after i looked out of the window, if I am still on earth ...
What I try to point out is, there is no magic behind, but be warned, if you think you are smart enough to touch the framework code (why and how ever you want to do that). Big cache gets you back where you are, at beginners level.

List of reserved words in rails *3*

Is there a good, authoritative list of reserved words for RAILS-3?
Candidates:
http://oldwiki.rubyonrails.org/rails/pages/ReservedWords, but it seems a bit out of date and rails2.
http://cheat.errtheblog.com/s/rails_reserved_words (but there seems no authority to this - it could just grow and grow...)
http://latheesh.com/2010/02/02/rails-reserved-words/
Background: I'm maintaining a long-serving rails app and it has plenty of usages of reserved words (judging that http://oldwiki.rubyonrails.org/rails/pages/ReservedWords seems to apply to rails2). However, none of these are actually interfering with current activity (the app works... and the specs&features I'm slowly adding to all pass). But as time passes I'd like to remove those usages of reserved words, but don't want to bother if some reserved words are no longer really reserved. So while a longer list might be good for NEW rails apps, I need stronger justification for budget spend than "it has been listed on a webpage at somepoint"...)
Maybe the nature of rails is that you can't find an authoritative list, but you can find "things that didn't work for me at some point"....
This seems like a pretty comprehensive list. However, you are correct. The very nature of programming means that this list will be ever changing. However I will do some research...maybe there is a site that can list reserved words based on the version of Rails you are using. If not...maybe someone should get working on it :D
Here's a good collaborative list of reserved words in Rails: https://reservedwords.herokuapp.com/

RoR: how can I seperate a page on my website into two large columns?

I want to make a vertical line going through the middle of the site and then have content on either side. How can I use CSS or ruby? to do this? I am not sure which one I would need and where I would put it. Also, what is the best resource for learning the syntax of the ruby on rails views/CSS stuff. It seems that rubyonrails.org doesn't have much documentation on that (they mostly explain the models and controllers)
I would suggest you start with something like: https://github.com/softmachine/rails-bootstrap
They provide a link to http://twitter.github.com/bootstrap/ which has plenty of documentation.
The next step, would be to ask a more specific question related to the exact problem you're having.
From your description it sounds like you need css, and depending on the nature of of the information you want to display, you might need to use ruby/rails to make it happen. Most likely, you could just use css.
see: http://jsfiddle.net/aTUq8/

What if the model in the ActiveRecord is an always plural word?

I am developing a Rails app which should rely on existing database.
There are a couple of table names there which are the always plural words, like "Series".
Application is not working correctly with the models associated with them. How would you propose to deal with it - is there any solution without changing the naming?
Thanks in advance!
It sounds like you need to tell Rails that "Series" is uncountable - that is, that it shouldn't try inflecting it for singular/plural. To do this, add the line inflect.uncountable 'series' to your config/initializers/inflections.rb file.
Curiously, however, "series" appears to be uncountable by default; did you just pick it as an example out of a number of similar names?