Skip to content

Instantly share code, notes, and snippets.

@devjj
Forked from radar/campaign.md
Created April 23, 2012 21:44
Show Gist options
  • Save devjj/2474073 to your computer and use it in GitHub Desktop.
Save devjj/2474073 to your computer and use it in GitHub Desktop.

Campaigning Thingo

The models you have are: Campaign, User, Card, Question and Response. Here's how I would do it using ActiveRecord

Campaigns exist with questions and responses by users. The Campaign model would look like this:

class Campaign < ActiveRecord::Base
  has_many :questions
  has_many :responses, :through => :questions
end

This means that the questions table has a campaign_id on it, and responses would have question_id. The Question model looks like this:

class Question < ActiveRecord::Base
  belongs_to :campaign
  has_many :responses
end
class PredefinedAnswer < ActiveRecord::Base
  belongs_to :question
end

Responses belong to a user, and also a question. The Response model:

class Response < ActiveRecord::Base
  belongs_to :record
  belongs_to :question
  belongs_to :predefined_answer
end
class Record < ActiveRecord::Base
  belongs_to :user
  belongs_to :campaign
end

The User model looks like this:

class User < ActiveRecord::Base
  has_many :responses
end

I don't know what cards have to do with any of this, since these are represented in the physical world only, it seems. Perhaps this could be represented in the database too, but I don't see a need for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment