Nice!
对于系统来说,重构只要一个标准,那就是不能出错也不能遗漏。 对于人来说,需要去考量输入(即消耗的资源,如时间)与输出,并且很多时候,代码具有很强的主观性,你所认为好的代码对于其他人来说也许并不完全是。
其实你可以在 helper 里面写个 binding.pry,然后看一下 self,你会发现它的 context 其实就是 ActionView
我试过很多编辑器,但是最终还是选择了 CKEditor,因为人家做了很久,稳定强大。
#15 楼 @lotusroot 我不是高手,这是数据库基础,AR 只是一个 ORM,提供了一套 DSL
分页跟你用什么没有关系,
至于分页插件,现在比较流行的是 kaminari,通过 AR 的 DSL,你可以直接这样
Order.join("LEFT OUTER JOIN products ON (products.id = orders.product.id)")
.join("LEFT JOIN stores ON (stores.id = orders.store_id)")
.select("stores.store_name AS store_name")
.select("products.product_name AS product_name")
.select("SUM(orders.rmb AS amount)")
.group("stores.store_name, products.product_name")
.order("stores.store_name DESC, products.product_name DESC")
.page(params[:page])
.per(30)
而如果自己写 SQL,然后用 ActiveRecord::Base.connection.execute
的话,你需要自己在 SQL 里管分页,而页面的显示,可以参考一下我最近写的博文 Custom Pagination in Rails Use Kaminari
#13 楼 @lotusroot 你理解错 ActiveRecord
的 includes
,好好看看这里的文档
你应该这样
Order.join("LEFT OUTER JOIN products ON (products.id = orders.product.id)")
.join("LEFT JOIN stores ON (stores.id = orders.store_id)")
.select("stores.store_name AS store_name")
.select("products.product_name AS product_name")
.select("SUM(orders.rmb AS amount)")
.group("stores.store_name, products.product_name")
.order("stores.store_name DESC, products.product_name DESC")
而且,按照你这个需要来看,根本就不是 Order 该管的事情,你应该直接写 SQL
report = ActiveRecord::Base.connection.execute <<-EOF
SELECT stores.store_name AS store_name,
products.product_name AS product_name,
SUM(orders.rmb) AS amount
FROM orders
LEFT JOIN products ON (products.id = orders.product_id)
LEFT JOIN stores ON (stores.id = orders.dim_store_id)
GROUP BY stores.store_name, products.product_name
ORDER BY stores.store_name DESC, products.product_name DESC
EOF
report.to_a.each do |row|
puts "#{row["store_name"]} - #{row["product_name"]} - #{row["amount"]}"
end
又或者去建一个 VIEW
最后一点:能看一下写 markdown 的语法么?代码应该用什么包起来?
模型的字段,是由其查询后的结果动态生成的,也就是说:如果你返回了 a b c 三个字段,那它即有 a b c 三个方法,而在另一个地方,你返回了 a b c d 那么它就拥有了 a b c d 四个方法。
A.select("table_a.a, table_a.b, table_a.c")
.select("table_b.d")
另外,你可以自己写 SQL 语句来做复杂的查询
result = ActiveRecord::Base.connection.execute <<-EOF
select table_a.a, table_a.b table_a.c, table_b.d, sum(column) as xxxx
from table_a
inner join table_b on (.......)
EOF
还可以写视图,然后建一个 model 来跟它对应
class Report < ActiveRecord::Base
self.table_name = 'xxxx_view'
end
写的人挺爽的,后来维护的人天天问候前面写的人
#3 楼 @5swords 决定用 Sequel
来替换 Source Model
那边的 AR
,然后把不兼容的代码全部 Fix 掉。
但是在自己拼 SQL 代码的时候仍然需要注意时区的问题,因为不经过 ORM 这一层;还有使用它的一套 DSL 的时候,还是要去检查一下究竟有没有帮你自动转换好的。
https://github.com/jeremyevans/sequel http://sequel.jeremyevans.net/documentation.html http://sequel.jeremyevans.net/plugins.html http://rosenfeld.herokuapp.com/en/articles/2012-04-18-getting-started-with-sequel-in-rails
# config/initializers/setup_sequel.rb
connection = ActiveRecord::Base.configurations["source_#{Rails.env}"]
connection["logger"] = [
Rails.logger,
Logger.new("log/source_#{Rails.env}.log")
]
Sequel.application_timezone = :local
Sequel.database_timezone = :utc
Sequel::Model.db = Sequel.connect(connection)
Sequel::Model.db.sql_log_level = Rails.application.config.log_level || :info
Erb 我用的是 Erb Snippets
监听下载链接事件,先异步发个请求到后台服务器,然后不要 e.preventDefault()
逻辑决定代码,所以你可以分为几块,登录的逻辑、社区板块的逻辑等等
不爽的一点就是,在 current
目录,它把版本管理给移除掉了。
#6 楼 @yangman_wenzhu 那重新 build 你的 ruby 吧
#2 楼 @yangman_wenzhu 你这回复里贴的图,不是已经 require 成功了么?
重新安装一遍试试,我本地在 irb 里试了一下,没有问题
本质上,其实都是执行 JS 代码。
如果用了 RJS,虽然说,能够很方便地直接根据逻辑然后执行相关的 JS 代码, 但是这样会把原本应该在同个地方的代码拆分出来了。 你要看什么逻辑,还必须得到模板里去看, 这个跟把代码逻辑直接全部交由前端 JS 代码还是有区别的, 逻辑不会太多还好,逻辑要是多了,那代码阅读起来就比较困难了,更别说维护代码了。
def current_company
# @current_company = current_user.company
# 换成下面会更好
@current_company ||= current_user.company
end
其他地方直接调用 current_company
而不是 @current_company
建议不要直接改写 model 的 default_scope,直接写个 concerns,然后让 ActiveRecord::Base 去 include 它就好了
module CurrentCompany
extend ActiveSupport::Concern
module ClassMethods
def on_company(company)
where(company_id: company.id)
end
end
end
ActiveRecord::Base.send :include, CurrentCompany