I don’t use build_product_price because I am not able to use it with
single table inheritance - e.g. ProductPrice is a “base class” and it’s
extended by NetPrice, MarkupPrice, MarkdownPrice, etc. How do I pass the
actual implementation (class type) to the build_xxx method?
Anyways, why I brought the whole thing up is because it’s kind of
awkward to have another instance variable for nesting. There is a hint
of this in the agile book (table on p. 343) where the last row uses
user[address][city]=Wien for form parameters. That is translated to
{:user => {:address => {:city =“Wien”}}} and then used as
user.update_attributes(params[:user]).
However, I was neither able to get the form helpers to produce the
nested field nor use the update_attributes
Hi Mark,
the code really works - I guess the first statement will create and save
product_price object (which doesn’t have any database reference on
Product) and the second will just create product and associate it with
ProductPrice…
build_net_price, etc. doesn’t work - I get NoMethodError
Thank you very much for the rest of the explanation on the nested
associations…I spent a few days on it not being able to work it out
But isn’t it something very common and natural to use associations in
object oriented design?
I didn’t know there was an update_attributes method for belongs_to
associations. Does this code work?
I don’t use build_product_price because I am not able to use it with
single table inheritance - e.g. ProductPrice is a “base class” and it’s
extended by NetPrice, MarkupPrice, MarkdownPrice, etc. How do I pass the
actual implementation (class type) to the build_xxx method?
You should be able to use build_net_price, build_markup_price, and
build_markdown_price.
Anyways, why I brought the whole thing up is because it’s kind of
awkward to have another instance variable for nesting. There is a hint
of this in the agile book (table on p. 343) where the last row uses
user[address][city]=Wien for form parameters. That is translated to
{:user => {:address => {:city =“Wien”}}} and then used as
user.update_attributes(params[:user]).
However, I was neither able to get the form helpers to produce the
nested field nor use the update_attributes
Yes, p.343 does imply that update_attributes works for nested
parameters. Though it’d probably work if address was an
aggregration, it won’t work for associations. Perhaps you should
submit an erratum at Pragmatic Bookshelf: By Developers, For Developers .
Nested parameters also don’t work with form helpers unless you
use a patch I’ve placed on the dev site. Even then, the
update_attributes and new methods won’t work unless you fiddle
with the params to properly build the object chain.
–
We develop, watch us RoR, in numbers too big to ignore.
I didn’t know there was an update_attributes method for belongs_to
associations. Does this code work?
the code really works - I guess the first statement will create and
save
product_price object (which doesn’t have any database reference on
Product) and the second will just create product and associate it
with
ProductPrice…
Yes, I see now that product_price is just an AR object for which
update_attributes works in its normal way. Associations just have
some extra or overridden methods besides the normal ones. Useful to
keep in mind.
Peter B. wrote:
build_net_price, etc. doesn’t work - I get NoMethodError
OK, I thought you had definitions “belongs_to :net_price”, etc.
–
We develop, watch us RoR, in numbers too big to ignore.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.