@blacktulip eval is evil, don't introduce it when you can.
好吧。可以做一个 duck typing.
module StudentFinder
GRADUATION_TYPE = "研究生"
def student_condition_table
raise "Override me"
end
def masters
where("check_status < ? AND #{student_condition_table}.stu_type = ?", 1, GRADUATION_TYPE)
.include(:“#『student_condition_table』”)
end
end
class CourseUser < ActiveRecord::Base
extend StudentFinder
def self.student_condition_table
"courses"
end
end
# Usage
masters = CourseUser.masters
本科生和研究生是人的属性,放到 User 里面会感觉更自然些,为什么要放到 course 里面。
你至少在这些 model 内部先做点工作吧,把这个 stu_type 代理出去。比如
class CourseUser
def stu_type
course.stu_type
end
end
这个只是方法之一。貌似表设计得也不是很合理。
主要不知道你那些patent
, research
什么的是干什么的。第一接口不统一不好抽象,第二像这个au.course.stu_type
本身就违反了 oop 原则。
要重构就先写测试吧。不写也不知道你要干什么,重构了也不能保证没问题。
基本上写好了之后好的代码自然就出来了。
不服不行啊
然后突然发现邻居开的都是 RAILS4,RUBY21, GITHUB,JSLINT 什么的
忘了就忘了,为了学 Ruby 不用把 Java 捡起来。一个动态一个编译差别很大的。
一般写的和见到的都是不同的组合比如 where, order, include, join 等等。
全部 where 的话应该不需要这么串,直接一个 where(params)就行了吧,空的自然不会出现。
具体还得看代码,不同需求不同方法。
总是跟 Java 比写出来的代码会成为 Ruva。
可以的。实际一点没什么不好的。需求来了再改。
如果你的 app 只需要是否登录这个权限,这样写足够好了。如果需要更多一点,helpers 慢慢地就会满天飞得招架不过来了。
def update(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save
end
end
Yes, two steps. The last step is save
.
如果你有 GUI 对应的肯定是用表,不论表是大是小。因为管理员肯定只能从界面上修改,不可能跑到程序里面改代码。
"Who wrote this code?" -- Said by a company which doesn't use GIT.
默认就有啊。 tail log/development.log
@ane 开个玩笑的。
终于明白楼主的险恶用心了,我还居然认真地回复了。原来楼主的潜在含义是想说他自己技能很强。
我觉着 attr_reader 或者 attr_accessor 是为了方便你使用变量的。你如果有大量变量,真的没有必要设置 attr_reader, 直接 foo.instance_variable_get("@a')
就行了。
@5swords 还不清楚楼主的具体情况,他说得很模糊。
Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy looks like: Reply < Message < ActiveRecord::Base, then Message is used to guess the table name even when called on Reply.
http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name
你如果没有自己写 table_name,那肯定就是一张表。
如果 column 大量重复,你当然是用一张表罗,常用的做法是设置一个 type, 就是 5s 说的 porlymorphic。
如果 column 很少重复的,那。。。那真的没有必要继承。Use composition or duck typing。
总共只有一张表,继承来继承去还是那张表上面的数据,能有什么问题?
楼主初期不用搞得很全面。首先简单设立一个 serialized column 就可以了。
的确过时了,现在是所有 view 可见。我上次还回了个帖子,现在找不到了 -- 话说回帖记录没有 pagination 真是不方便。
看似简单其实不简单。搞清楚 Query 和 Command 都需要一些功底的。
下划线需要去掉。这条函数接受的是外部参数,下划线会引起混淆。即使是在 Rails 里面,下划线的变量也是少数,代表给库用的而一般不给用户使用。
任何程序,写成一串when "星期一": piuts "今天是星期一"
都是该批评的。
简单一点可以这样
return puts puts "今天周末" if data == "星期六“ || data == ”星期天“
puts ”今天#{data}“
另一个问题是,即使是 demo, 输出的几个部分都毫不相干。应该分成几个函数。
同学,你多虑了。
你这个点子的问题是永远依赖于微博。