我正在查看Rails的ActiveSupport扩展,遇到了“输入?”字样。方法。对我来说,它的外观和工作方式与“ include?”完全相同 方法,但只是相反。
([1..5]).include? 1
1.in? ([1..5])
Run Code Online (Sandbox Code Playgroud)
我一直在使用“包含?” 自从我第一次开始使用Rails以来,这种方法非常有趣,因为还有另一种方法可以做完全相同的事情。我缺少的两种方法有什么区别吗?
编辑
在任何情况下都使用“输入”吗?会比使用“包含”受益更多??因为现在,我只能想到的是“在吗?” 给“ include”一个相当无意义的方法是什么?为此目的已经存在。
在IRB中,如果我运行以下命令:
require 'date'
Date.today
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
=> #<Date: 2015-09-26 ((2457292j,0s,0n),+0s,2299161j)>
Run Code Online (Sandbox Code Playgroud)
但是在Rails控制台中,如果我运行Date.today
,我得到这个:
=> Sat, 26 Sep 2015
Run Code Online (Sandbox Code Playgroud)
我查看了Rails的Date类,但无法找到Rails如何Date.today
以不同于Ruby的输出显示输出.
任何人都可以告诉Rails如何Date.today
或Date.tomorrow
格式化日期以便很好地显示?
Ruby 中有一个“super”关键字,它会遍历祖先链,以便找到链上的第一个方法实现并执行它。所以,这就是 Ruby 中的工作方式,毫不奇怪:
module Mammal
def walk
puts "I'm walking"
end
end
Run Code Online (Sandbox Code Playgroud)
require '~/Documents/rubytest/super/mammal.rb'
class Cat
include Mammal
def walk
super
end
end
Run Code Online (Sandbox Code Playgroud)
2.7.0 :001 > simba = Cat.new
2.7.0 :002 > simba.walk
I'm walking
=> nil
Run Code Online (Sandbox Code Playgroud)
这是理想的行为。现在,Rails 中的 ActiveSupport::Concern 为模块提供了一些额外的功能。如果您使用 ActiveSupport 帮助程序以类似的方式进行操作,则会发生以下情况:
module MammalMixin
extend ActiveSupport::Concern
included do
def show
@mammal = Mammal.find(params[:id])
end
end
end
Run Code Online (Sandbox Code Playgroud)
class SomeController < ApplicationController
include MammalMixin
def show
super
end
end
Run Code Online (Sandbox Code Playgroud)
如果您到达该控制器,则会出错: super: no superclass method `show' for #SomeController:0x000055f07c549bc0
当然,可以不使用“included …
ruby ruby-on-rails activesupport super activesupport-concern
我的 Rails 版本升级有点晚了。让我惊讶的是 Rails 生成的 config/environment/* 文件中需要一堆 active_support 。
它们是做什么用的?这和Rails6中引入的Zeitwerk有什么关系吗?我不记得它们出现在旧版本的 Rails 中。
ag ^require config/environments
config/environments/development.rb
1:require "active_support/core_ext/integer/time"
config/environments/test.rb
1:require "active_support/core_ext/integer/time"
config/environments/production.rb
1:require "active_support/core_ext/integer/time"
Run Code Online (Sandbox Code Playgroud)
重现步骤:
rails new myapp
cat Gemfile | grep "^gem 'rails'"
gem 'rails', '~> 6.1.3', '>= 6.1.3.2'
Run Code Online (Sandbox Code Playgroud)
我试图在 Rails/rails CHANGELOG 和一些 git blaiming 中找到此更新,但这没有帮助。
首先,我没有使用Rails.我正在使用Sinatra进行Active Record的这个项目.
我希望能够在我的Model类上覆盖to_json或as_json,并让它定义一些'default'选项.例如,我有以下内容:
class Vendor < ActiveRecord::Base
def to_json(options = {})
if options.empty?
super :only => [:id, :name]
else
super options
end
end
end
Run Code Online (Sandbox Code Playgroud)
其中Vendor具有的属性多于id和name.在我的路线中,我有以下内容:
@vendors = Vendor.where({})
@vendors.to_json
Run Code Online (Sandbox Code Playgroud)
这@vendors
是一个Array供应商对象(显然).但是,返回的json不会调用我的to_json
方法并返回所有模型属性.
我实际上没有选择修改路线,因为我实际上使用的是修改过的sinatra-rest gem(http://github.com/mikeycgto/sinatra-rest).
有关如何实现此功能的任何想法?我可以在我的sinatra-rest gem中做类似下面的事情,但这看起来很傻:
@PLURAL.collect! { |obj| obj.to_json }
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下2个型号的Rails 3.1应用程序
class Listing < ActiveRecord::Base
has_many :listing_saves
end
class Team < ActiveRecord::Base
has_many :listing_saves
has_many :saved_listings, through: :listing_saves, source: 'listing'
end
Run Code Online (Sandbox Code Playgroud)
Join模型看起来像这样
class ListingSave < ActiveRecord::Base
belongs_to :team
belongs_to :listing
end
Run Code Online (Sandbox Code Playgroud)
Mow我认为存在一个变形问题,因为每当我尝试运行我的测试时,我都会收到以下错误(这是一个错误示例和导致它的测试)
it "should return the listing saves associated with the team" do
save = Factory :listing_save, listing: @listing, saver: @user, team: @team
@team.listing_saves.should include save
end
Failures:
1) Team listing_saves associations should return the listing saves associated with the team
Failure/Error: @team.listing_saves.should include save
NameError:
uninitialized constant Team::ListingSafe …
Run Code Online (Sandbox Code Playgroud) ruby-on-rails inflection activesupport ruby-on-rails-3 ruby-on-rails-3.1
我很高兴和惊讶地发现ActiveSupport以我想要的方式完成月份总和.无论在这几个月中有多少天,添加1.month
到某个特定的日期Time
都会让您在同一天的日子里登陆Time
.
> Time.utc(2012,2,1)
=> Wed Feb 01 00:00:00 UTC 2012
> Time.utc(2012,2,1) + 1.month
=> Thu Mar 01 00:00:00 UTC 2012
Run Code Online (Sandbox Code Playgroud)
activesupport提供的months
方法Fixnum
没有给出线索:
def months
ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
end
Run Code Online (Sandbox Code Playgroud)
按照...中的+
方法Time
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
else
plus_without_duration(other)
end
end
Run Code Online (Sandbox Code Playgroud)
...使我们since
在Fixnum
...
def since(time = ::Time.current)
time + self
end
Run Code Online (Sandbox Code Playgroud)
......这导致我们无处可去.
ActiveSupport(或其他)如何/在哪里进行聪明的月份数学而不是仅仅添加30天?
我做了sudo apt-get install rails
,一切都很好.
Reading package lists... Done
Building dependency tree
Reading state information...
...
Setting up ruby-actionmailer-2.3 (2.3.14-2) ...
Setting up ruby-activeresource-2.3 (2.3.14-1) ...
Setting up ruby-rails-2.3 (2.3.14-2) ...
Setting up rails (2.3.14.1) ...
Setting up ruby1.8-dev (1.8.7.352-2ubuntu0.1) ...
Run Code Online (Sandbox Code Playgroud)
但是现在当我尝试做的时候
铁路新测试
我明白了 cannot load such file -- active_support (LoadError)
在Rails 3.2.12中,当我运行rails s
它时抛出
/usr/local/share/gems/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- bigdecimal/util (LoadError)
Run Code Online (Sandbox Code Playgroud)
我甚至为Gemfile添加了gem'bigdecimal',但我仍然得到同样的错误,任何指针?
我正在编写一个涉及日期比较的 Ruby 脚本。
\n为了保持它的可读性,我想做类似的事情1.day.ago
。我认为这就像添加gem \'activesupport\'
到我的 gemfile 并要求require \'active_support\'
. 但这是行不通的。
我更进一步:
\nrequire "active_support/core_ext/date/calculations"\nrequire "active_support/core_ext/integer/time"\nrequire "active_support/core_ext/time"\n
Run Code Online (Sandbox Code Playgroud)\n但我还没有完全做到这一点:
\n1.day.ago\n#NameError: uninitialized constant ActiveSupport::IsolatedExecutionState\n#\n# ::ActiveSupport::IsolatedExecutionState[:time_zone] || zone_default\n
Run Code Online (Sandbox Code Playgroud)\n\xe2\x80\xa6I\ 不确定我还需要什么。如何在我的普通 ruby 脚本中使用所有活动记录的日期/时间方法?
\nactivesupport ×10
ruby ×6
activerecord ×1
date ×1
inflection ×1
rubygems ×1
sinatra ×1
super ×1
zeitwerk ×1