Is there a difference between the two โbelongโ statements
Two operators define the same relationship. The difference is how certain associations refer to the class that defines these associations.
The assign_to call defines the full impact of the new instance methods for the calling class to handle this relationship. These new methods are called based on the first argument to property_to. As with most other Rails, ActiveRecord will use reasonable default values โโderived from the first argument to generate methods that control the association. We provide additional arguments to simply override these default values.
In fact, they are identical in all but the name. It all comes down to your personal preferences. Would you rather refer to the association with @model.keeper or @model.staff ?
Read the attribute_to section of the Associations for a better understanding of the methods provided when the model belongs to another, and how options that belong to the _ class are used.
None of these statements differ in how they will give you methods in the class you are using. Suppose you define these statements inside an Office class, so the first statement, i.e.
class Office < ActiveRecord::Base belongs_to :keeper, :class_name => "Staff" end creates a relationship between the Staff class and the Office class, but the relationship between Office and Staff is called the custodians and will use keeper_id as a foreign key in the Office class to establish relationships. Therefore, if you have an instance of the Office object in the office variable, you can get the Personnel for this office object using
office.keeper In another scenario
class Office < ActiveRecord::Base belongs_to :staff, :foreign_key => "keeper_id" end you create a relationship between the staff and the Office class, and the relationship between the office and staff is called the staff and will use keeper_id as a foreign key in the Office class to establish the relationship, because you specified the foreign key.
So, in this case, if you want to get into the state, then you will do
office.staff In general, if you just do
belongs_to :staff in your model, then according to the rails convention, you will get a personal method in your model that will use staff_id as a foreign key to establish the relationship between the State model and this. But rails also give you the opportunity to redefine these conventions in exceptional circumstances, such as an outdated application. So basically you redefine the rails convention in the above two statements using the foreign key and class name.
Both codes do a similar thing, but there is a difference. In the case of the first line, the rails guess that the Staff link is executed using keeper_id , it just adds _id . In the second case, you specify where it should look for the link by specifying the attribute :foreign_key .
The difference may be in how you relate to the class to which you want to belong. In the first case, it will be XY.keeper , in the second - XY.staff . It is up to you which form you prefer.
Although both methods are equivalent in terms of defining relationships, with the main difference being the name, the โbestโ of the two is the one you are currently using the most. This is a matter of coherence more than anything else.
You want to refer to things like:
object.keeper Or more wise to use:
object.staff My personal preferences relate to the first form, but, as a rule, are clear, I express all the quirks of the relationship, if this is unusual in any respect. The definition will be:
belongs_to :keeper, :class_name => 'Staff', :foreign_key => :keeper_id Outlining the nature of the relationship in full, the amount of confusion that may arise is minimized.