arr.each_with_object(Hash.new(0)) { |v, h| h[v[0]] += v[1].to_i }
支持
3.times do
2
end
1 if not_solved?
高深莫测
+1
+1
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 23878 | 13391 | 370 | 1760 | 4 | 5 |
| Helpers | 2381 | 1989 | 1 | 300 | 300 | 4 |
| Models | 21610 | 14640 | 335 | 1894 | 5 | 5 |
| Mailers | 745 | 625 | 27 | 91 | 3 | 4 |
| Javascripts | 18787 | 14934 | 35 | 2317 | 66 | 4 |
| Libraries | 702 | 552 | 14 | 71 | 5 | 5 |
| Controller specs | 11811 | 10239 | 2 | 17 | 8 | 600 |
| Decorator specs | 476 | 409 | 0 | 0 | 0 | 0 |
| Feature specs | 1082 | 910 | 0 | 1 | 0 | 908 |
| Form specs | 1505 | 1363 | 0 | 0 | 0 | 0 |
| Helper specs | 156 | 63 | 0 | 0 | 0 | 0 |
| Lib specs | 48 | 41 | 0 | 0 | 0 | 0 |
| Mailer specs | 393 | 329 | 0 | 0 | 0 | 0 |
| Model specs | 18993 | 13438 | 0 | 10 | 0 | 1341 |
| Policy specs | 36 | 31 | 0 | 0 | 0 | 0 |
| Presenter specs | 239 | 214 | 0 | 0 | 0 | 0 |
| Query specs | 174 | 156 | 0 | 0 | 0 | 0 |
| Request specs | 713 | 600 | 0 | 0 | 0 | 0 |
| Serializer specs | 784 | 419 | 0 | 0 | 0 | 0 |
| Service specs | 5376 | 4762 | 1 | 15 | 15 | 315 |
| Uploader specs | 51 | 41 | 0 | 0 | 0 | 0 |
| Validator specs | 75 | 64 | 1 | 1 | 1 | 62 |
| Value specs | 268 | 225 | 0 | 0 | 0 | 0 |
| View specs | 7107 | 6745 | 0 | 4 | 0 | 1684 |
| Worker specs | 114 | 101 | 0 | 0 | 0 | 0 |
| Wrapper specs | 32 | 26 | 0 | 0 | 0 | 0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total | 117536 | 86307 | 786 | 6481 | 8 | 11 |
+----------------------+-------+-------+---------+---------+-----+-------+
Code LOC: 46131 Test LOC: 40176 Code to Test Ratio: 1:0.9
分母+1
我以为会一直嵌套呢
如果有 A 任务耗时多了怎么办 如果有 A 任务失败了怎么办
其实直接在 rails route 里面把你 record 的路由全部写出来就好了 一旦有新的记录就重新 reload 一次,
match your_record_url, to: 'controller#action', via: :all, defaults: { id: record_id }
如果 试着去用Rails内置的route方法解析和查找记录
或许可以这样用
ps: 如果路由记录太多话 可能需要考虑其他办法
require 'singleton'
require 'action_dispatch'
require 'active_support/all'
class RequestRoutes
include Singleton
def match(path, method)
memos = simulate.simulate(path).try(:memos)
return nil if memos.blank?
memos.reverse.find { |memo| memo[:request_method] == method }
end
def add_route(api_request)
append_to_ast(api_request)
clear_cache
end
def clear_cache
@simulate = nil
end
def reload
@ast = nil
@simulate = nil
end
private
def simulate
@simulate ||= begin
builder = ActionDispatch::Journey::GTG::Builder.new ActionDispatch::Journey::Nodes::Or.new ast
table = builder.transition_table
ActionDispatch::Journey::GTG::Simulator.new table
end
end
def ast
@ast ||= []
# 这里可以把你所有的记录全部转换成ast
# YourRecord.all.map do |api_request|
# parse_to_nodes(api_request)
# end
end
def parse_to_nodes(api_request)
memo = {
request_id: api_request[:id],
request_method: api_request[:method],
pattern: ActionDispatch::Journey::Path::Pattern.from_string(api_request[:url])
}
nodes = parser.parse (api_request[:url])
nodes.each { |n| n.memo = memo }
nodes
end
def append_to_ast(api_request)
@ast << parse_to_nodes(api_request)
end
def parser
ActionDispatch::Journey::Parser.new
end
end
request = {
id: 1,
method: 'GET',
url: '/publishers/:p_id/magazines/:m_id/photos/:p_id'
}
RequestRoutes.instance.add_route(request)
memo = RequestRoutes.instance.match('/publishers/1/magazines/2/photos/1', 'GET')
match_date = memo[:pattern].match('/publishers/1/magazines/2/photos/1')
match_date.names.zip(match_date.captures).to_h
吱
老婆是做测试的 经常会跑去 TesterHome 逛逛 赞一个!
和 〈转〉重构臃肿 ActiveRecord 模型的 7 种方式 [翻译] 好像 对照起来看 哈哈
赞! 有没有电子版?喜欢用 kindle 走哪看哪
膜拜一下
一份迟了两年多才看到的帖子 赞一个
class Book < ActiveRecord::Base
has_many :book_tags
has_many :tags, through: :book_tags
end
class Tag < ActiveRecord::Base
has_many :book_tags
has_many :books, through: :book_tags
end
class BookTag < ActiveRecord::Base
belongs_to :tag
belongs_to :book
end
book = Book.create
tag = Tag.create
book.tags << tag || tag.books << book
没有创建额外的表。 guide
按照你的写法,默认会需要两个字段 imageable_id
和 imageable_type
1.你的第二种做法只是建立起了关联关系,如果你觉得这样用方便,你依然可以这么用
2.至于为什么推荐使用多态关联,你首先需要知道什么叫多态
可以屏蔽不同对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化
第一种做法 从Picture
的角度来看关联对象,你不需要去关注是关联了 product 还是关联了 employ
你不需要通过增加字段来适应新的关联关系
awesome
不知道我理解的对不对
result = Array.new(4) { [] }
@projects.each_with_index do |v, i|
result[i % 4] << [v, i]
end
.tile.is-ancestor
.tile.is-3.is-vertical
.tile.is-parent.is-vertical
- result.each do |project|
.title.is-child.box
p.title = project[1]
相当于遍历两次@projects
#10 楼 一语道破了天机么
以前为了学习Callbacks
参照它的逻辑写了一个迷你版的 demo
因为每个 Hint 可以属于某个 Player 也可以不属于
所以你不能这样写
has_many :hints, through: :players
可以
class Hint < ApplicationRecord
belongs_to :player
belongs_to :game
end
@jasl 感谢大大鼓励,不过 01:34 还没睡,注意休息啊
帽衫 👍
@gyorou 3Q 感谢推荐
@matrixbirds 我貌似加过你 qq, 不过在 at 你的时候 发现有好多类似的名字,你是『下面』君么?