Ok. Case. I have one duck, which has many ducklings… Some of these
ducklings ceases to exist (dies) or duck gives birth to new ducklings…
However… A duckling is not an empty entity. There are some constraints
in the db which we must conform to. So. Whenever a duck dies or new ones
gets born, details needs to be provided before we actually want to
persist it to the database. So i can’t do:
@duck = Duck.find(:first)
@duck.ducklings << Duck.new
…since i’ve observed that the new duck is persisted right away. This
violates some constraints and i do not want to persist the duck yet.
Furthermore. A duck usually gives birth to more than one duckling at a
time, so we want to manipulate a lot of ducklings from the same form at
the same time. If i remove a duckling, if i add a duckling or if i alter
a duckling’s information, i first want this to be persisted when i hit
update.
Now this has caused me some issues…
Loading the duck and listing form rows for duck.ducklings gives me text
fields with: ducklings[1][name], ducklings[1][birth_date]… That is.
Form submitted back is translated into a hash with key matching the db
id…
However. Now i click add and a new row is created via RJS from a new
instance of Duckling… However. Since this is not persisted, i’ll now
have a row-field with id: ducklings[][name]… Hitting update will now
cause an application error:
Conflicting types for parameter containers. Expected an instance of
Array, but found an instance of Hash. This can be caused by passing
Array and Hash based paramters qs[]=value&qs[key]=value.
Well. I do understand the error message, however. Expected rails to have
some magic in this sense giving me a cleaner view, such that non-keyed
entries were treated as new entries thus would yield a save instead of
update.
Do i really have to distinguish new from existing entries by alternative
field names? Aren’t there any prettier ways to do this?
Haven’t been able to find anyone discussing this particular issue… What
is the rails way? I’d really like that new and old persisted entries
came back in the same params collection in some way.