# Tricky design problem (collection\_select and Javascript)

**URL:** <https://www.ruby-forum.com/t/tricky-design-problem-collection-select-and-javascript/154922>\
**Category:** Rails\
**Created:** [December 5, 2008, 7:22am UTC](https://www.ruby-forum.com/t/tricky-design-problem-collection-select-and-javascript/154922 "2008-12-05T07:22:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [December 5, 2008, 7:22am UTC](https://www.ruby-forum.com/t/tricky-design-problem-collection-select-and-javascript/154922/1 "2008-12-05T07:22:36Z")

</div>

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  
have)

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.
