How can I create has_one through an association?

For an association, has_oneI can build this association:

foo.build_bar()

How do I create an association has_one, through:?

For example:

class Foo

  has_one :bar
  has_one :baz, through: :bar

end

How do i create baz? This example foo.build_bazgets the value No Method Error.

The documentation here says:

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 = {})

However, this does not seem to be the case. Using Pry to introspect an instance Foo, I see that such a method is not added, as it would have been has_onewithout through:.

+4
source share
1

, :

foo.build_bar().build_baz()
foo.save!
+3

All Articles