views:

37

answers:

1

Hey Guys,

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?

Cheers,

Stuart

A: 

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.

lakshmanan
Thanks for your answer lakshmanan, but I think you may have misunderstood my question which is based on the principal of allowing users to create forms that will be used by other users.
stuartchaney
Still no takers?...
stuartchaney