Jeroen Houben wrote:> When using a belongs_to (or any association type for that matter) how do 
> you assign the associated object?
> 
> Example:
> 
> Employee belongs to a Company
> 
> do you do this:
> 
> e = Employee.new
> e.company_id = 12
> 
> or
> 
> e = Employee.new
> e.company = Company.find(12)
Definitely the second. You''re correct in finding it more reliable. But 
also, it''s a practice belonging to a larger principle: code to an 
interface, not an implementation.
By accessing e.company_id you''re violating its encapsulation barrier. 
(In other words, you''re poking around in its internals, when you should
ask the object in question to do it for you.)
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---