开发者

Resources that best explain associations used in Rails

I am coming from a Java background where we write our own queries or stored procedures and deal directly with low level database stuff.

I am going though AWDR 3rd edition book and can not grasp the joins concept.

Can someone share a 开发者_C百科resource they used when first coming to Rails?

Let's say I want a Customer table and Order table. a customer can place many orders. So will my Customer Model class contain :has_many order_id

If I do the above, will it make foreign/primary keys in the actual database?

Now let's say I want to find all customers which order with id 5.

How will I do that?

Is it good to just go with find_by_sql?


Take a look at http://guides.rubyonrails.org/association_basics.html

It has a pretty good explanation of Rails Associations, how they work, why you need them, and how to use them.


I think I can give you a quick rundown.

You're used to dealing with SQL directly, and the nice thing is that Rails promotes good database design. If you have a books table and a chapters table, then a book will have many chapters, correct? So just as in other database setups, the chapter table should have a foreign key telling what book it belongs to.

Assuming you have this foreign key in the chapters table, you can setup your Book model like so:

# app/models/book.rb
class Book < ActiveRecord::Base
  has_many :chapters
end

You're telling Rails that a book literally has many chapters. So if you have a book object, you can say book.chapters and that will return an array of the chapter objects that belong to this book. Rails will know to look at the chapters table for the book_id foreign key, no custom SQL required.

Conversely, assuming this same database setup, you can define your chapter model to be associated with books, like this:

# app/models/chapter.rb
class Chapter < ActiveRecord::Base
  belongs_to :book
end

Notice :book here is singular, just an idiom that helps readability, since a chapter can only belong to a single book. Now if you have a chapter object and say chapter.book, it will return the book object that the chapter belongs to.

Just as in other database applications, the foreign key is always on the "belonged to" table.

I hope this helps!


As suggested the Ruby On Rails Guides is a good place to start, along with the RailsCast site

The key point is that the foreign keys, although implied by the use of the ActiveRecord abstractions such as :belongs_to, :has_many, :has_and_belongs_to_many etc, are not created for you. In the Rails Guides notes on Associations you'll notice comments like:

"In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations."

This note is often referenced in the details of the individual associations under the :foreign_key option.

I guess it's often not obvious and from my experience (esp. coming from a Java/C++ background) I found that I had to print out and read the set of guides a couple of times to really appreciate the elegance, simplicity as well as the wealth of information contained within them.

On a side note - it's great not to have to deal with all the low level plumbing as per the Java experience.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜