Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails guides document on Activerecord association incorrect?

Wow I've been struggling with this for whole day, following the "official" ruby on rails guides document, and just discovered that I might have been misguided by the document all along. I just want to confirm if this is true.

If you go to http://guides.rubyonrails.org/association_basics.html and under 2.10. self joins section it says:

class Employee < ActiveRecord::Base
  has_many :subordinates, :class_name => "Employee"
  belongs_to :manager, :class_name => "Employee",
    :foreign_key => "manager_id"
end

Now, I'm a newbie and just believed in this code (What else can I do?) and wrote some code that's a variation of this self join case. However the more I looked at it the more it didn't feel right. isn't :subordinates supposed to have the :foreign_key field instead of :manager? Anyway I've just changed it so that the code is something like:

class Employee < ActiveRecord::Base
  has_many :subordinates, :class_name => "Employee", :foreign_key => "manager_id"
  belongs_to :manager, :class_name => "Employee"
end

and now it's working. Am I missing something? Or is the official document wrong? It's hard to believe that the official document would present incorrect information but maybe that's the way it is.

like image 411
Vlad Avatar asked Mar 23 '12 06:03

Vlad


People also ask

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

What is Active Record in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

How do I validate in Ruby on Rails?

This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the :with option. Alternatively, you can require that the specified attribute does not match the regular expression by using the :without option. The default error message is "is invalid".

What is polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.


1 Answers

That's right, the guide document is incorrect at the time of this writing.

The belongs_to doesn't need the :foreign_key option because AR will infer manager_id from the name of the association ("manager"). As documented, AR would raise an error when, given an Employee @dwight one attempts to @dwight.subordinates, because AR would use employee_id in the WHERE condition of the SELECT statement.

According to the AR documentation passing the :foreign_key option to has_many results in declaring the FK that will be used when generating the query for @dwight.subordinates.

like image 117
ybakos Avatar answered Nov 09 '22 04:11

ybakos