- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先让我为一个看似简单的问题道歉,但作为 Rails、Ruby 和编程新手,我觉得我已经用尽了“Rails 新手”教程。
这就是我所面临的问题。
我有一个用户模型和机构模型,它们具有“has_many :through => :company_reps”关系。
用户具有基本字段(姓名、电子邮件、密码)(我正在使用 devise )
机构有很多字段,但相关字段是(客户 = bool 值、领导 = bool 值、demo_date = 日期/时间)更复杂的是,每个机构可以拥有一到两个用户,但大多数只有一个。
我们正在为用户举办一场竞赛,我需要根据 demo_date 字段和 client 字段向每个用户奖励积分。
因此,首先我需要做的是为每个用户提供与作为客户的机构相关的 10 分,除非该机构有 2 个用户,在这种情况下,我需要为这两个用户各提供 5 分。
其次,我需要为与演示日期在 2012 年 2 月之后的机构相关的所有用户提供 1 分。
我正在使用 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
最佳答案
更新(因为我的第一次尝试错误地假设 User has_one :institution
最简单的选择是对 Institution
进行基本计算模型来建立机构“值得”多少积分,然后将该值相加来计算用户的积分。
# Institution
def points
points_for_client + points_for_demo_date
end
private
def points_for_client
if client?
10 / users.count
else
0
end
end
def points_for_demo_date
if demo_date.present? && demo_date >= Date.new(2012, 3, 1)
1
else
0
end
end
请注意,您可以压缩这些 if
使用三元运算符将语句转换为单行语句 ? :
如果你更喜欢。另请注意,我假设“2 月之后”的意思是“从 3 月 1 日起”。
支票为零 demo_date
也是一个品味问题。从中选择
# Verbose, but IMO intention-revealing
demo_date.present? && demo_date >= Date.new(...)
# Perhaps more idiomatic, since nil is falsy
demo_date && demo_date >= Date.new(...)
# Take advantage of the fact that >= is just another method
# Concise, but I think it's a bit yuk!
demo_date.try :>=, Date.new(...)
既然每个机构都有一定的分数,那么将它们总结起来就相当简单了:
# User
def points
institutions.inject(0) {|sum, institution| sum + institution.points }
end
查看the docs for inject
如果您不熟悉它,这是一个漂亮的小方法。
就性能而言,这并不是最理想的。一个基本的改进是记住结果:
# Institution
def points
@points ||= points_for_client + points_for_demo_date
end
# User
def points
@points ||= institutions.inject ...
end
以便进一步调用points
在同一请求中不要重新计算该值。只要 client
就可以了和demo_date
User
时请勿更改对象仍然存在:
some_user.points #=> 0
some_user.institution.client = true
some_user.points #=> 0 ... oops
User
对象将在下一个请求时重新创建,因此这可能不是问题(这取决于这些字段如何变化)。
您还可以添加 points
字段到 User
从而将该值保存在数据库中,使用原始版本作为 update_points
方法代替
def update_points
self.points = institutions.inject ...
end
但是,确定何时重新计算该值将成为一个问题。
我的建议是尽可能保持简单并避免过早优化。这是一个相对简单的计算,因此只要您没有大量的用户和机构或大量的请求,它就不会成为一个大的性能问题。
关于mysql - 在 Ruby 和 Rails 中使用计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13053111/
目前我正在构建相当大的网络系统,我需要强大的 SQL 数据库解决方案。我选择 Mysql 而不是 Postgres,因为一些任务需要只读(MyISAM 引擎)而其他任务需要大量写入(InnoDB)。
我在 mysql 中使用如下命令。当它显示表格数据时,它被格式化为一个非常干净的表格,间距均匀且 |作为列分隔符。 SELECT * FROM TABLE_NAME; 当我从 CLI 运行命令时,如下
我知道这个问题之前已经被问过好几次了,我已经解决了很多问题,但到目前为止没有任何效果。 MySQL 试图将自身安装到的目录 (usr/local/mysql) 肯定有问题。关于我的错误的奇怪之处在于我
以下是我的 SQL 数据结构,我正在尝试如下两个查询: Select Wrk_ID, Wrk_LastName, Skill_Desc from Worker, Skill where
我们有一个本地 mysql 服务器(不在公共(public)域上),并希望将该服务器复制到我们拥有的 google 云 sql 实例。我的问题是:1.这可能吗?2.我们的本地服务器只能在本地网络上访问
我有一个表(test_table),其中一些字段值(例如字段 A、B 和 C)是从外部应用程序插入的,还有一个字段(字段 D),我想从现有表(store_table)插入其值,但在插入前者(A、B 和
我想创建一个 AWS RDS 实例,然后使用 terraform 管理数据库用户。因此,首先,我创建了一个 RDS 实例,然后使用创建的 RDS 实例初始化 mysql 提供程序,以进一步将其用于用户
当用户在我的网站上注册时,他们会在我的一个数据库中创建自己的表格。该表存储用户发布的所有帖子。我还想做的是也为他们生成自己的 MySql 用户——该用户仅有权从他们的表中读取、写入和删除。 创建它应该
我有一个关于 ColdFusion 和 Mysql 的问题。我有两个表:PRODUCT 和 PRODUCT_CAT。我想列出包含一些标记为:IS_EXTRANET=1 的特殊产品的类别。所以我写了这个
我想获取 recipes_id 列的值,以获取包含 ingredient_id 的 2,17 和 26 条目的值。 假设 ingredient_id 2 丢失则不获取记录。 我已经尝试过 IN 运算符
在 Ubuntu 中,我通常安装两者,但 MySQL 的客户端和服务器之间有什么区别。 作为奖励,当一个新语句提到它需要 MySQL 5.x 时,它是指客户端、服务器还是两者兼而有之。例如这个链接ht
我重新访问了我的数据库并注意到我有一些 INT 类型的主键。 这还不够独特,所以我想我会有一个指导。 我来自微软 sql 背景,在 ssms 中你可以 选择类型为“uniqeidentifier”并自
我的系统上有 MySQL,我正在尝试确定它是 Oracle MySQL 还是 MySQL。 Oracle MySQL 有区别吗: http://www.oracle.com/us/products/m
我是在生产 MySQL 中运行的应用程序的新维护者。之前的维护者已经离开,留下的文档很少,而且联系不上了。 我面临的问题是执行以下请求大约需要 10 秒: SELECT COUNT(*) FROM `
我有两个位于不同机器上的 MySQL 数据库。我想自动将数据从一台服务器传输到另一台服务器。比方说,我希望每天早上 4:00 进行数据传输。 可以吗?是否有任何 MySQL 内置功能可以让我们做到这一
有什么方法可以使用 jdbc 查询位于 mysql 根目录之外的目录中的 mysql 表,还是必须将它们移动到 mysql 根目录内的数据库文件夹中?我在 Google 上搜索时没有找到任何东西。 最
我在 mysql 数据库中有两个表。成员和 ClassNumbers。两个表都有一个付费年份字段,都有一个代码字段。我想用代码数字表中的值更新成员表中的付费年份,其中成员中的代码与 ClassNumb
情况:我有 2 台服务器,其中一台当前托管一个实时 WordPress 站点,我希望能够将该站点转移到另一台服务器,以防第一台服务器出现故障。传输源文件很容易;传输数据库是我需要弄清楚如何做的。两台服
Phpmyadmin 有一个功能是“复制数据库到”..有没有mysql查询来写这个函数?类似于将 db A 复制到新的 db B。 最佳答案 首先创建复制数据库: CREATE DATABASE du
我有一个使用 mySQL 作为后端的库存软件。我已经在我的计算机上对其进行了测试,并且运行良好。 当我在计算机上安装我的软件时,我必须执行以下步骤: 安装 mySQL 服务器 将用户名指定为“root
我是一名优秀的程序员,十分优秀!