哪种款式首选?一个与另一个有充分的理由吗?
提前致谢!
1) cmds.each do |cmd|
end
2) cmds.each { |cmd|
}
Run Code Online (Sandbox Code Playgroud)
示例代码:
cmds = [ "create", "update", "list", "help" ]
# Block style one
#
cmds.each do |cmd|
puts "loop1, cmd: #{cmd}"
end
# Block style two
#
cmds.each { |cmd|
puts "loop2, cmd: #{cmd}"
}
Run Code Online (Sandbox Code Playgroud) 大家好我想创建一个范围,找出所有0地址的联系人.Got error message ArgumentError: tried to create Proc object without a block在rails c中运行命令'Contact.noaddress'时.
这是我的联系模式,包括范围
class Contact < ActiveRecord::Base
attr_accessible :email, :firstname, :lastname, :mobilephone, :fullname
has_many :addresses
validates_presence_of :firstname, :lastname
scope :noaddressed, lambda do |addresses|
joins(:addresses).where('addresses.created_at.empty?', true)
end
end
Run Code Online (Sandbox Code Playgroud)
这是地址模型
class Address < ActiveRecord::Base
attr_accessible :city, :country, :postalcode, :region, :street
belongs_to :contact
end
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?非常感谢.
我的应用程序中有许多多行ActiveRelation查询方法,我不确定编写这些方法的最惯用方法.看看这个例子:
def postal_code_ids_within(miles)
nearby_postal_codes = PostalCode.where("latitude > :min_lat and latitude < :max_lat",
min_lat: (latitude - (miles.to_f / MILES_PER_DEGREE_LATITUDE.to_f / 2.to_f)),
max_lat: (latitude + (miles.to_f / MILES_PER_DEGREE_LATITUDE.to_f / 2.to_f)))
nearby_postal_codes = nearby_postal_codes.where("longitude > :min_lon and longitude < :max_lon",
min_lon: (longitude - (miles.to_f / MILES_PER_DEGREE_LONGITUDE.to_f / 2.to_f)),
max_lon: (longitude + (miles.to_f / MILES_PER_DEGREE_LONGITUDE.to_f / 2.to_f)))
nearby_postal_codes.pluck(:id)
end
Run Code Online (Sandbox Code Playgroud)
对我来说感觉有点不对劲.从中返回ActiveRelation对象的块似乎是惯用的,但我还没有看到这种方法.
什么是标准?