First let me apologize for a seemingly easy question, but being new to rails, ruby and programming I feel like I've exhausted the "New to Rails" tutorials out there.
Here's what I'm up against.
I have a Users model and Institution Model that have a "has_many :through => :company_reps" relationship.
The user has basic fields (name, email, password) (I'm using devise)
The Institution has many fields but the relevant ones are (client = boolean, lead = boolean, demo_date = date/time) To complicate it further each Institution can have one or two users but most only have one.
We are holding a contest for the users and I need to award points to each user based on the demo_date field and client field.
So firstly what I need to do is give each user 10 points that is related to an institution that is a client, unless that institution has 2 users in which case I need to give those two users 5 points each.
Secondly I need to give all users 1 point that are related to an institution that has a demo date after Feb. 2012.
I'm using Ruby 1.9.2, Rails 3.2.8 and MySQL
- So, how can I accomplish this?
- Should I create a new table and model to store the points, if so how do I save the calculations?
- Should I put all the calculations in the User or Institution Model?
As always thank you for the help.
MySQL Institution Info
CREATE TABLE `institutions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_id` int(11) DEFAULT NULL,
`company` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`clientdate` datetime DEFAULT NULL,
`street` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`source2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`demodate1` datetime DEFAULT NULL,
`demodate2` datetime DEFAULT NULL,
`demodate3` datetime DEFAULT NULL,
`client` tinyint(1) DEFAULT NULL,
`prospect` tinyint(1) DEFAULT NULL,
`alead` tinyint(1) DEFAULT NULL,
`notcontacted` tinyint(1) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7805 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Institution Model
class Institution < ActiveRecord::Base
attr_accessible :company, :phone, :assets, :clientdate, :street, :city, :state_id, :zip, :source, :source2, :demodate1, :demodate2, :demodate3, :client, :prospect, :alead, :notcontacted
belongs_to :state
has_many :users, :through => :company_reps
has_many :company_reps
end
User Model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name
# attr_accessible :title, :body
has_many :states, :through => :rep_areas
has_many :institutions, :through => :company_reps
has_many :rep_areas
has_many :company_reps
def name
first_name + " " + last_name
end
end
Company Rep Model
class CompanyRep < ActiveRecord::Base
belongs_to :user
belongs_to :institution
end