Reputation: 5539
I just recently inherited a Rails application and am debating an architectural decision moving forward. Here's a bit of background... Feedback appreciated.
There are, currently, 16 different ad types, all with a set of the same attributes, several with one or two additional attributes, and a couple with three or four.
Currently each ad type has a separate model and table with the respective columns, and a separate controller and views in a CMS too used for basic CRUD. The CMS takes advantage of inherited_resources, so that limits some of the duplication.
I wrote out the attribute set -- there are 20 or so that cover all ad types. Certain ad types have associations -- several has_many associations where the foreign key is stored on the associated table and a few belongs_to.
The ads have no real behavior. Specific ad types are simply displayed on various pages, so as long as I can determine if there is an ad of a specific type, we're golden.
I am debating moving to a single model, table, and controller, but want to get as much input as possible from the stackoverflow community on whether or not this is 1) a good fit for the problem 2) any performance concerns 3) any potential programming bottlenecks I haven't though of. Here is my thinking so far...
Assuming a route /:location/:ad_type/new (e.g. /homepage/ad_type_1/new):
The ad_controller would create @ad with ad_type set to params[:location] + params[:ad_type] and render the new view which would contain a series of conditionals for displaying the appropriate partials for the given ad_type. Submit would fire the create action creating the ad with the expected attributes defined for the ad type, one of which in this case would be ad_type = homepage_ad_type_1.
I've not thought as much about retrieving the data, but assuming the ad_type column is set correctly, I should be able to create an "of_type" scope that pulls records based on the ad_type column. Not sure if I'm missing something there.
Validations will vary based on the ad_type value.
I'm not entirely certain how many ads will exist at any given time, but I feel this could be addressed at a later time. Either by moving off some of the stale rows to an ad_archives table or similar.
Your thoughts are appreciated. Thanks.
Upvotes: 1
Views: 162
Reputation: 8240
I would probably use a single table model for this, but if it makes sense to use multiple controllers/views, you don't have to restrict yourself to a single controller, and you can still query ads based on their type without using conditionals. Example:
create_table :people do |t|
t.string first_name
t.string last_name
# some other properties...
t.string type
end
class Student < Person
end
class Teacher < Person
end
Person.all # shows all students and teachers
Teacher.all # shows all teachers
So it would be easy to make a Teachers controller and a Students controller, as well as a People controller.
Upvotes: 1