首先,让我为一个看似简单的问题表示歉意,但对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'这个假设。我会更新我的答案。 –