Multi Table Inheritance with rails 3 - ruby-on-rails

Are there standards or best practices yet when it comes to multi table inheritance in rails 3? So far the best article I could find was:
http://mediumexposure.com/multiple-table-inheritance-active-record/
But even that needed some changes(e.g. moving the requires to an initializer instead of the old /config/environment.rb)
Any better resources / standards?

For an easy way to do multi-table inheritance take a look at 'acts_as_relation' plugin https://github.com/hzamani/acts_as_relation.

I recently forked a promising gem to implement multiple table inheritance and class inheritance in Rails. I have spent a few days subjecting it to rapid development, fixes, commenting and documentation and have re-released it as CITIER (Class Inheritance and Table Inheritance Embeddings for Rails).
Consider giving it a look: https://github.com/PeterHamilton/citier
It actually takes some concepts from that article you mention.

There's a guy in the Melbourne Ruby group I attend that's written a couple of blogs on table inheritance in rails and the comments are really helpful as well. It's not specifically Rails 3 but there's definitely some decent pointers in there.
http://rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading
http://rhnh.net/2010/07/02/3-reasons-why-you-should-not-use-single-table-inheritance
happy reading

Related

Tutorial for Creating Views for ActiveRecord Associations?

In teaching Rails students I often arrive at the point where they've
created two models and set up an association and are wondering how they can
make that association visible in the views.
Now my question is, are there any overview articles / tutorials / blog posts for this,
which give an overview of the different ways to show Associations in the views.
For example, I see several "Association Patterns", that are, a
1:n association could be Aggregation or Composition as described in the UML
with different implications for creation of associated entities (which might
point to nested resources, only discussed in terms of routing)
... or something like belonging to a category etc. where you would want to choose
from existing options or create a new one if the desired entity is not found;
which is handled by first_or_create, but again, I haven't seen any documentation
summarizing different options for that in the view (as RailsCast #57 and 258 describe, for example.)
I have tried searching for it. Also, neither Michael Hartl's Tutorial or the PragProgs "Agile Web Development with Rails" have something like that.
So, is there something out there and if not, do people think it would it be useful to create it?
For a more OO / less rails-way discussion of presenting object graphs in views, check out Avdi Grimm's discussion of what he calls the Exhibit pattern:
http://objectsonrails.com/#ID-2656c30c-080a-4a4e-a53e-4fbaad39c262
This has great examples and a good discussion of where and why it might be a good design decision not to follow the pure rails way, if your students are interested in going beyond the API.
Some Railscasts:
Checkboxes for has-and-belongs-to-many (n:m) relations: http://railscasts.com/episodes/17-habtm-checkboxes
Nested forms: http://railscasts.com/episodes/196-nested-model-form-revised
I wrote a gem for doing nested forms (cocoon), and documented how to solve all types of nested forms in the wiki of the gem. See https://github.com/nathanvda/cocoon/wiki/A-guide-to-doing-nested-model-forms
Hope this helps.
You might want to have them check out the Draper gem. It has excellent documentation and screencasts:
https://github.com/jcasimir/draper
It may not be exactly what you are looking for, but it will teach them about decorator patterns.
I hope this helps!

Rails: Act_as_taggable_on vs. Rocket_tag

Looks like Act_as_taggable_on is the more established gem, but Rocket_tag is the newer and hotter one.
Which one would you recommend, and why?
Most of the posts I found that compared Rails tagging were fairly outdated, and I was wondering if anyone knew something about Rocket_tag (not much info on it out there).
I used both.
I am very happy with act_as_taggable, but for Rails 3.1 I switched to rocket_tag.
On every project that is in rails <3.1 act_as_taggable.
Sorry, I do not have any oline resources, just my own opinion.
I'm the author of rocket_tag so I'm a bit biased. The reason I wrote rocket_tag was that I found the code for the other taggable gems to be messy and unreadable and thus hard to extend and play with. I generally use Ernie Millers excelent Squeel GEM for writing and composing database queries. Squeel is a super clean and powerful ruby DSL for putting together SQL.
If you have a need for some custom tagging query then I suggest taking a look at how rocket_tag is put together and trying to roll your own tagging analysis method.

Advanced Ruby on Rails database?

Im taking on an upcoming project that involved creating and managine a large complex mysql database using RoR. It involved, many table, deep foreign keys, many to many, etc.
Anyone know a good resource (book, website, etc.) which can help me learn how to do it? I need a clearer understanding of migrations and how rails handles relational databases.
Any suggestions?
railscasts.com is great. He talks a lot about setting up your typical HABTM relations polymorphic associations, inner nestings you name it. and Bates makes it extra easy to understand.
Past that I would definately try to start off on the right food using the Metrics gem to monitor database and CPU usage. Always ensuring you're making the lightest touch on the db.
Besides RailsCast, I would recommend RailsLab: Scaling Rails which focus on large Rails application. It seems doesn't update recently, but the post still useful, not only database, but also memory, performance etc.
+1 for Railscasts.com suggestion from Trip. I've gotten a ton out of those since I moved to Ruby and Rails from ColdFusion. I also like the book "The Rails 3 Way" from Obie Fernandez. It has an extensive ActiveRecord section that is very informative both for new Rails users and is a great reference. Another solid one is "Rails 3 In Action" from Ryan BIgg and Yehuda Katz.
You can check out books at the Pragmatic Programmers website. Probably one of the best options is: Agile Web Development with Rails
I also found Ruby on Rails Tutorial to be a good resource.
Also be sure to look at the Rails Guides They are well written and cover many topics in depth. For example, the migrations you mentioned and many to many associations You can even download them for offline use. Here is an answer about how to do that here
Code School have some great interactive courses such as Rails for Zombies.

Tutorial teaching Ruby syntax/knowledge required to easily learn rails

I am trying to learn ROR these days and have basic knowledge of ruby, but often working with rails, I get to the point where it seems as if I don't know a bit about ruby.
Just to explain the point, in rails we use has_many keyword. I did not learn any such thing when I was going through ruby tutorials but just came to know that it has something to do with meta-programming in ruby (I have no idea what is meta programming).
So I would like to know if there is any book/tutorial which explain all the points/syntax/concepts of ruby, which a newbie would see while programming in rails.
Thanks to "Jonas Elfström", in simple words, what I am looking for is to know "how Rails uses Ruby"
Thanks.
has_many isn't a keyword, it's simply a class method in the ActiveRecord::Associations module.
It's documented here and you can even view the source if you scroll down a bit.
Associations are a set of macro-like class methods for tying objects
together through foreign keys. They express relationships like
“Project has one Project Manager” or “Project belongs to a Portfolio”.
Each macro adds a number of methods to the class which are specialized
according to the collection or association symbol and the options
hash. It works much the same way as Ruby’s own attr* methods.
If you already know Ruby the Rails Guides could get you going but it might be easier to learn from one of the books listed at the documentation page or by watching a couple of screencasts.
For books about Ruby I've never seen such praise as what Eloquent Ruby gets.
There's a tutorial called Learn Ruby the Hard Way
(written by Rob Sobers of Fog Creek Software)
I tried lately this: http://pine.fm/LearnToProgram/ which helped me a lot because there are one or two concepts that had slipped. I started long ago with an arcane version of this.

Ruby Rails _without_ ActiveRecord

I'm looking for any pointers on how to write a rails web app without ActiveRecord.
A doc or an example of a (not too complex) web app using storage backends other than a relational database would be greatly appreciated.
It's not clear on what should be implemented in the model classes in order to make the rails app work without the ActiveRecord layer.
Thanks,
Of course it's possible, here, for example, MongoMapper is used instead of ActiveRecord:
http://railstips.org/blog/archives/2009/07/23/getting-started-with-mongomapper-and-rails/
Note that this will seem a lot easier with Rails 3. Rails team spent a lot of efforts on ORM agnosticism when pushing to beta. They've created a public API (ActiveModel) for different ORMs to implement, so that an ORM can serve as a drop-in replacement for ActiveRecord. That way you'll just be able to define models in terms of your ORM without any extra efforts.
DataMapper already has an implementation of ActiveModel in dm-rails, and there'll be more to come.
See this post by Yehuda Katz for details.

Resources