首先,讓我爲一個看似簡單的問題表示歉意,但對rails,ruby和編程新手我覺得我已經用盡了「New to Rails」教程。在Ruby和Rails中使用計算
這就是我所反對的。
我有一個「has_many:through =>:company_reps」關係的用戶模型和機構模型。
用戶有基本的字段(姓名,電子郵件,密碼)(我使用devise)
該機構有許多領域,但相應的有(客戶端=布爾值,鉛=布爾值,demo_date =日期/時間) 使每個機構進一步複雜化可以有一個或兩個用戶,但大多數只有一個。
我們爲用戶舉辦比賽,我需要根據demo_date字段和客戶端字段向每個用戶頒發積分。因此,首先我需要做的是爲每個用戶提供10個與作爲客戶的機構相關的點數,除非該機構有2個用戶,在這種情況下,我需要給這兩個用戶各5分。
其次我需要給所有用戶1點涉及到具有2012年二月之後演示日期
我使用Ruby 1.9.2的機構來說,Rails 3.2.8和MySQL
- 那麼,我該如何做到這一點?
- 我應該創建一個新的表格和模型來存儲點,如果是的話我該如何保存計算?
- 我應該把所有的計算都放在用戶或機構模型中嗎?
一如既往感謝您的幫助。
MySQL的機構信息
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;
機構模式
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
用戶模型
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
公司代表型號
class CompanyRep < ActiveRecord::Base
belongs_to :user
belongs_to :institution
end
哇...完美。非常感謝。 – DaveG
還有一個問題。當我做一個rails控制檯來嘗試代碼。我會做一些像u = User.find(1)。然後u.points我得到未定義的方法'客戶端?' 。這是由於用戶擁有多個機構嗎? – DaveG
啊,對不起,我誤解了這個問題,我正在研究'User has_one:institution'這個假設。我會更新我的答案。 –