Reputation: 432
Hope someone can help me out. Currently working on a RoR project that needs a strange kind of functionality.
Basically, I have two types of users named User and Researcher. Users can create forms and these forms are stored in the db and filled in by researchers.
I have this basic schema
create_table "form_fields", :force => true do |t|
t.string "name"
t.string "form_field_type"
t.integer "form_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "forms", :force => true do |t|
t.integer "visit_id"
t.string "name"
t.integer "form_category_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "researchers", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "researchers", ["email"], :name => "index_researchers_on_email", :unique => true
add_index "researchers", ["reset_password_token"], :name => "index_researchers_on_reset_password_token", :unique => true
create_table "results", :force => true do |t|
t.integer "form_id"
t.integer "subject_id"
t.string "form_field_name"
t.string "form_field_value"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
create_table "visits", :force => true do |t|
t.string "name"
t.integer "visit_order"
t.integer "study_id"
t.datetime "created_at"
t.datetime "updated_at"
end
So, a User creates the forms and form_fields and a researcher then logs in and is provided with this form
<% form_for [:researcher, @study, @subject, @visit, @form, Result.new] do |f| %>
<% @form.form_fields.each do |form_field| %>
<%= f.label form_field.name, :index => form_field.id %>
<%= f.hidden_field :form_field_name, :value=>form_field.name, :index => form_field.id %>
<%= f.text_field :form_field_value, :index => form_field.id %><br/>
<%= f.hidden_field :form_id, :value=>@form.id, :index => form_field.id %>
<%= f.hidden_field :subject_id, :value=>@subject.id, :index => form_field.id %>
<% end %>
<%= f.submit %>
<% end %>
So the result is stored in the results table and thats that.
I can see myself running into trouble when trying to allow users to set validations on each form_field.
Anybody have any experience with this problem?
Upvotes: 3
Views: 1578
Reputation: 637
Here's how I would handle this...
I would have a User
model that has_many
Form
models. Each Form model would have many FormComponents
. The FormComponent
model would have fields for what type of form element it is, what kind of validation, default values, etc.
The Researcher
would be served the form by the FormController
, which would process the model and display the form based on what FormComponents
were present. When a Researcher
completed the form, I would package the response as a FormResponse
that belongs both to that Researcher
and the Form
.
Keep in mind this is just a rough idea of how this idea would play out, but in my opinion a scheme similar to this would be the best way to handle it.
Upvotes: 1
Reputation: 10400
I would actually look into this problem more clean way Using Single Table inheritance.
You create a model User and define all the validations and methods common to both researcher and user in the user.rb file.
class User < ActiveRecord::Base
#all validations code and other logic related to users and researchers
end
Create another model researcher.rb like
class Researcher < User
#create validations and logic code specific to researcher
end
There will be just one table "users". And there will one extra columns (apart from all email, name, etc..,) that is "type" column.This type column refers to whether the record is researcher or user.
define some method in researcher.rb like
def can_fill_form?
return true
end
and make the same method in user.rb like
def can_fill_form?
return false
end
By doing this, you just have one table with all users and researchers in it. Things like email, phone, password are all the same and creating a separate type of table is redundant here.
Upvotes: 0