Reputation: 12429
I have a Business
model in my application, which will be storing information about
Some of the businesses share attributes/amenities:
But some are specific for a single type of businesses, for example restaurants:
I am wondering what would be the best way of modelling this information in the DB?
My current thinking:
Use a single table for Business
that will hold all the information and attributes. Most of the attributes are boolean, therefore whenever an attribute does not apply to a business type, it will simply be false
.
I am not sure however, if there is a better way of doing this. Also not sure about how to handle these differences at the view level. For example, when creating a business, I want the option the display/set those attributes that are applicable to the business type.
Upvotes: 1
Views: 78
Reputation: 629
That is the way I would do it. You should have an attribute called business_type
that lets you choose what attributes to display.
For example in the show view:
<% if @business.business_type == "restaurant" %>
Provides take out: <%= @business.take_out %>
<% end %>
You could use jQuery to change what fields are available on the new form based on what restaurant type it is:
<%= f.select :business_type, [["Restaurant", "restaurant"], ["Store", "store"]] %>
<script>
$('#business_business_type').change(function(){
// hide or show fields here based on this.value
});
</script>
If you want to avoid having a bunch of database columns, you could use the key-value store in rails 3.2 (3/4 of the way down this page):
Make a column called amenities
. Then in business.rb
:
store :amenities, accessors: [:wifi, :ac, :accessibility, ...]
Upvotes: 1