I have a product model with 4 attributes called price, price_per_unit, unit and unit_amount. I want to allow either the price attribute to be filled or the other 3 fields but not all 4 of them. Here’s a better view:
class Product < ActiveRecord::Base
attr_accessible :price, :price_per_unit, :unit, :unit_amount
end
<%= form_for(@product) do |f| %>
# Either give a regular price or....
<%= f.text_field :price %>
# a price per unit the requires all 3 of these fields.
<%= f.text_field :price_per_unit %>
<%= f.text_field :unit %>
<%= f.text_field :unit_amount %>
<%= f.submit %>
<% end %>
So in the database (or the website) if I look at a product its either by a per unit basis or has a regular price but cannot have both.
How would I go about doing this?
I would say that mostly depends on how you want the user interface to behave
In any case, you should have two parts: