HOWTO: Beginning Relationships
Part 1: "We met at a bar..."
Target Audience
Rails Beginner ~ You've got some knowledge of web applications, but are just starting your journey on Rails.
Introduction
The foundation of a great web application is proper relationships. The Rails framework takes most of the headache away from developing these relationships and almost entirely eliminates the need to write low-level queries to your database. If you've spent time writing those queries, Rails' ActiveRecord will make sense, but for those of you who are just getting started this can be confusing.
Demo Application Overview
In this table we'll be looking at designing our databases and defining relationships in the code. Our demonstration application will be an inventory system for a local pub. Here is an overview of our relationships:
Hopefully that is clear enough for us to get started.
Step 1: Creating our Rails app and Creating the Models
Last edited by Reedy (2006-08-02 12:36:30)
Offline
Great post! Thanks
I don't know if you want to move in to this, but something that I may try on my own is to use these relationships and test out the simply_restful additions to edge. I haven't seen many tutorials on this subject. It may be a path that you take with this tutorial if you want to go that way. Great job though!
Offline
Great tutorial, will be very useful for first-timers and even though I knew this stuff already your clear & concise writing style helped crystalise the information in my head. Good example data, something that people can relate to easily.
One error to correct, the 'The Drink and Container Relationship' part of your article seems to be wrapped in the code block where you output the brewery's drinks.
Awesome dude, look forward to part II
Offline
Thanks for pointing out the formatting error. It's been fixed. I appreciate the praises and hope to get started on part 2 soon.
Offline
Extremely helpful, thanks, but I would like to suggest that you let the code display fully as the print version still chomps some code.
I hope the follow up integrates acts_as_list with habtm!
And it would also be helpful to see some migration files pushing habtm join table data.
Last edited by xia (2006-08-25 05:33:37)
Offline
xia wrote:
I would like to suggest that you let the code display fully as the print version still chomps some code.
I think that's the fault of the current implementation of our code tag. I'll look into what we could do about that.
Offline
Great, thanks...
Is it possible to print the individual posts (without copy/paste of course !) ?
Last edited by blackflicker (2006-08-25 22:17:33)
Offline
A very helpful and well presented articly, many thanks - I was just thinking I would be using find_by_sql, since I wasnt making much sense of joins in rails eg What is the meaning of Firm#clients in the api documentation?
I am however struggling with using some of the methods such as minimum on joined tables. eg, I have a bookingrefs and bookings table with a one to many on id/bokingref_id. bookings has a column datebooked. and I would like to find minimum databooked for a specific id. Maybe these methods might feature in your part II (assuming they cab be used across joins)
Offline
OK so it didnt work before, but my post, I tried again and it worked.
In case anyone is intereste I did this:
@bookingref=Bookingref.find_by_id(26)
earliest=@bookingref.bookings.minimum('hiredate)
or without the intermediate variable:
Bookingref.find_by_id(26).bookings.minimum('hiredate)
Now that is neat, even though possibly a bit unreadable.
Offline
Reedy,
Useful guide. Thanks. Two points:
1. You assume the reader knows how to make the migration tables. However, I didn't know that tables that belong_to require an id for what they belong to. For example, drinks requires a 'brewery_id'. A newbie might not know that.
2. The :through part doesn't work. I get "Invalid source reflection macro :has_and_belongs_to_many for has_many :drinks, :through => :tab. Use :source to specify the source reflection."
Would be intersted in seeing the source.
-Andrew Roth
Offline
and is this out of date now ?
Code syntax changes and :through seems to be the next big thing but very little on it is here
Offline
Things move fast in Rails world. I have not updated this code to make use of :through as it is not necessary for the tutorial project. In future versions I will include an example of :through.
I'm working on the second part currently, so be sure to check back. I'll be updating the existing code base and providing a downloadable copy as well.
Offline
Although :through is becoming a common alternative to has_and_belongs_to_many, I think they both still have their place. It all depends upon what you need.
Offline
Reedy wrote:
I'm working on the second part currently, so be sure to check back.
If you get it in by Oct. 31st it'd be eligible for the tutorial contest. 
Offline
Very nice and clear.
I am currently facing a challenge related to rendering partials - I have a simple data structure.
Users, Personalinfos, and Pictures.
User {
has_many:picture
has_one:personalinfo
}
Personalinfo {
belongs_to:user
validates_associated:user
}
Picture {
belongs_to:user
validates_associated:user
}
I have a file - index.rhtml - I want to display login form and a list of existing users - So I rendered a partial _list.rhtml - which contains some html tags (formatting info) - now from within _list.rhtml i have this statement -
<%=render_collaction_of_partials "list_profiles", @userprofiles%>
in view _list_profile.rhtml - when i try to access personalinfo (e.g. list_profiles['personalinfo].firstname) nil object is returned - I have checked the db the data is there and the controller does populate @userprofiles (e.g. @userprofiles[1].personalinfo.nil? = false) - in the controller i have the following statement
@userprofiles = User.find(:all)
so now my question is why the statement <%= list_profiles['personalinfo'].firstname %> returns nil.firstname error?
thanking in anticipation
Last edited by dagger007 (2006-12-10 21:25:21)
Offline
What’s with you guys can’t release a tutorial that isn’t fraught with errors and omissions?
It’s a total disservice to anyone trying to learn rails.
Why not publish working code you’ll cut the number of posts in half.
Offline
You should also keep in mind that this tutorial was published on August 1st and some things in Rails have changed since then.
Offline
besides the fact that nothing if perfect - tutorials should always contain errors for the people who want to use it - so that they could THINK their way out of it 
Offline