我需要在我的Rails应用程序中使用一个百分比.在任何视图中,包括用户输入的时间,格式都需要为数百种格式100.000
.当它用于计算时,它需要以百分之一格式表示,1.00000
.
我的迁移(我将列添加到现有表中)具有以下行:
add_column :worker, :cash_split, :decimal, :precision => 6, :scale => 5
Run Code Online (Sandbox Code Playgroud)
所以,截至目前,我正在以百分之一(1.00000
)格式存储它.我选择以这种格式存储它的基础是,worker.cash_split / 100.0.to_d
当我需要进行乘法时,我认为它将意味着更清晰的业务逻辑(即没有代码挂起).
我唯一的另一个想法是滥用compos_of方法.我可以将数据以数百(100.000
)格式存储为cash_split,然后创建一个以1.0000格式cash_split_percentage
返回的属性访问器cash_split
.
我认为我需要一些类似于rails eager加载查询的东西,但有限制,但我找不到解决方案.
为简单起见,我们假设系统中永远不会超过30 Person
秒(因此Person.all
是一个小数据集),但每个人将有超过2000条评论(因此Person.include(:comments)
将是一个大型数据集).
家长协会
class Person < ActiveRecord::Base
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
儿童协会
class Comment < ActiveRecord::Base
belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)
我需要查询Person
s 列表并包含它们comments
,但我只需要其中的5个.
我想做这样的事情:
有限的家长协会
class Person < ActiveRecord::Base
has_many :comments
has_many :sample_of_comments, \
:class_name => 'Comment', :limit => 5
end
Run Code Online (Sandbox Code Playgroud)
调节器
class PersonController < ApplicationController
def index
@persons = Person.include(:sample_of_comments)
end
end
Run Code Online (Sandbox Code Playgroud)
遗憾的是,本文指出:"如果您急于加载具有指定:limit选项的关联,它将被忽略,返回所有关联的对象"
这有什么好办法吗?或者我注定要在急切加载1000个不需要的ActiveRecord对象和N + 1查询之间进行选择?另请注意,这是一个简化的示例.在现实世界中,我将与其他相关联Person
,在相同的index
行动中具有相同的问题comments
.(照片,文章等).
我在多个环境中安装了postgres.在每个环境中,我都有2个以上的数据库.
如果我拥有超级用户数据库权限,是否有办法在给定的postgres安装上为所有数据库安装CITEXT扩展?
截至目前,一旦登录到环境和postgres控制台,我必须CREATE EXTENSION IF NOT EXISTS citext;
为每个数据库运行.
我对Ruby块和触发器的理解是它们都是闭包.现在我已经看到它与instance_eval一起使用,我有点困惑.什么是魔术酱,在查看裸机时的工作量不足,与使用instance_eval相比,它改变了块在大多数常见用途下的作用范围?
以下是您可以在IRB中转储以查看我的意思的示例.我已经包含了proc.call和block yield版本示例.令人高兴的是,他们的行为都是一样的.
# Testing block/proc and eval
class Example
def initialize(value)
# value defined in the instance
@value = value
end
def call_a_proc(proc)
proc.call self
end
def yield_to_block
yield self
end
end
# Value defined in the global object
@value = 1
example = Example.new 'a'
# the block/proc that prints @value
proc1 = -> instance { puts @value }
# instance calling/yielding the block/proc that prints @value
proc2 = -> instance { instance.call_a_proc proc1 }
proc3 = -> …
Run Code Online (Sandbox Code Playgroud) 问题
你好,
我没有运气试图将这个SQL语句分解为ActiveRecord/Rails友好代码,我想学习如何在这种情况下避免使用find_by_sql语句.
场景 我有用户在执行操作时创建审核.每次审核都是特定的audit_activity.根据score_weight,每个audit_activity值得一定数量的积分.我需要根据他们累计的audit_activity score_weights找到每个用户的总分.最终我需要对它们进行排名,这也意味着要对它进行排序.
我的代码 这是我的sql和有问题的表的简化版本.有什么想法吗?
带有完整列名的SQL(为清晰起见)
SELECT users.id, u.email, SUM(audit_activity.score_weight)
FROM users
JOIN audits ON users.id = audits.user_id
JOIN audit_activities ON audit_activities.id = audits.audit_activity_id
GROUP BY users.id;
Run Code Online (Sandbox Code Playgroud)
模型:用户,审计,AuditActivity
用户字段:id,email
class User < ActiveRecord::Base
include Clearance::User
has_many :audits
end
Run Code Online (Sandbox Code Playgroud)
审计字段:id,user_id,audit_activity_id
class Audit < ActiveRecord::Base
belongs_to :user
belongs_to :audit_activity
end
Run Code Online (Sandbox Code Playgroud)
AuditActivity字段:id,score_weight
class AuditActivity < ActiveRecord::Base
has_many :audits
end
Run Code Online (Sandbox Code Playgroud)
示例数据
这是一组SQL语句,因此您可以使用我正在使用的类似数据,并查看运行相关查询时出现的情况.您应该能够将整个事物复制/粘贴到数据库查询浏览器中.
CREATE TABLE users(
id INTEGER NOT NULL,
email TEXT (25),
PRIMARY KEY (id)
);
CREATE TABLE audits(
id INTEGER …
Run Code Online (Sandbox Code Playgroud)