Technically, belongs_to
will work without matching has_many
or has_one
. If, for example, you say that Order belongs_to :customer
, you can call .customer
on the Order object and get the Customer object. What you cannot do is call .orders
on the Client without telling him that he has_many :orders
(or .order
, in the case of has_one
), because this method is created by the has_many
declaration.
However, I cannot think of any reason why you would only like to indicate half the relationship. This is a terrible design choice, and you should not do that.
Edit: has_one
does not create .collection
methods that has_many
does. Per manual :
4.2.1 Methods added by has_one
When you declare a has_one association, the declaration class automatically receives four methods associated with the association:
association(force_reload = false) association=(associate) build_association(attributes = {}) create_association(attributes = {})
You will notice that there is no .new
on this list. If you want to add a related object, you can use customer.build_order()
or customer.order = Order.new()
.
source share