Forum: Ruby on Rails Tricky design problem (collection_select and Javascript)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Ram (Guest)
on 2008-12-05 07:22
(Received via mailing list)
Im cornered with this problem in one of my apps. Let me define the
models first.

Paycheck has_one salstruct, :class_name => 'Salary_Structure'
Paycheck belongs_to employee
Adjustment #a model for additions/deductions that do not fall into the
normal Salary Structure and are last minute add-ons to the Paycheck.
eg, Christmas bonus.

The Adjustments table has the columns Name (string) and Value (integer
in terms of percentage).

I allow 2 Adjustments for every Paycheck. The paycheck table has the
columns Adj_1 (string), Adj_2 (string), Adj_1_value (float),
Adj_2_value (float).

There is no association between Paycheck and Adjustment.

In the _paycheck partial (used for both editing and creating
paychecks) , I use the following code.

<%= f.collection_select :adj_1, current_user.adjustments.find
(:all) , :value, :name, {:include_blank=>true, :id =>
"paycheck_adj_1"},{:onblur => 'calculate();'} %>%

This code lets the user select the Adjustment by its name (say,
Christmas bonus, Leave Deduction, etc), but I have set the value that
gets into the DB as the 'value' attribute of the adjustment. I have
done this because i am using Prototype to perform some calculations on
the values of the adjustments chosen.
The following are my requirements,
- the 'name' attribute of the adjustment chosen by the user, should go
into paycheck_adj_1/paycheck_adj_2 respectively.
- the Prototype JS function 'calculate()' uses the 'value' attribute
of the chosen adjustment in the form of DOM_ID.value. Preferably, I
should not have to hack into that code too much as its working great
right now. And the results of these calculations are stored in
paycheck_adj_1_value/paycheck_adj_2_value respectively.
- If the User chooses to delete an Adjustment at any time from the
Adjustments table, It should not affect the Adjustments data stored
for issued Paychecks (which is why I have designed the DB the way I

Now, although I think Ive got the 3rd requirement covered, the first
two pose a problem in that I cant think of a way to satisfy both
simultaneously. I do hope though, that some of you can help me resolve
this problem. Will gladly explain more in detail if needed.
This topic is locked and can not be replied to.