Tag: activesupport

rails 3.1变形问题

我有一个Rails 3.1应用程序与以下2个型号 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 Join模型看起来像这样 class ListingSave < ActiveRecord::Base belongs_to :team belongs_to :listing end 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 […]

btw const_get和qualified_const_get有什么区别?

标准ruby库中有一系列方法,即const_defined?,const_get或const_set。 const_defined?, const_get, const_set 而且,在Rails的Active Support Core Extensions中,存在这些个体的“qualified_”对应物。 qualified_const_defined?, qualified_const_get, qualifeid_const_set 有没有人可以明确解释这些方法的裸形和合格forms之间的差异? 先感谢您。 孝

我如何需要ActiveSupport的rescue_from方法?

我在application controller有这个代码: # Method to capture and handle all exceptions rescue_from Exception do |ex| Rails.logger.debug ex do_stuff(ex) end 我想把它移到一个模块然后: class ApplicationController < ActionController::Base include 'module' … 现在我的模块看起来像: # lib/exception_mailer.rb require ‘action_mailer’ require ‘active_support’ module ExceptionMailer # Method to capture and handle all exceptions rescue_from Exception do |ex| … 我得到: undefined method ‘rescue_from’ for ExceptionMailer:Module 我用Google搜索了“我如何在模块中包含rescue_from?” […]

ActiveSupport中mattr_accessor和cattr_accessor之间的区别?

我无法通过查看源代码分别在Class和Module中提供的cattr_*和mattr_*方法之间的差异来解决这个cattr_* 。 我读到了这个问题: Rails模块中的mattr_accessor是什么? 这提供了有关这两种方法的一些细节,但没有强调差异。 所以我的问题是它们之间有什么区别?为什么我们在源代码中几乎完全相同时需要两套方法? 另外,我们应该在什么时候使用?

rails activesupport通知 – 错误的db运行时值

我正在尝试记录我的REST API应用程序的请求。 我正在使用rails通知,例如http://railscasts.com/episodes/249-notifications-in-rails-3 我无法理解如何解决rails通知的一个问题。 我的初始化代码 ActiveSupport::Notifications.subscribe “process_action.action_controller” do |name, start, finish, id, payload| p name p start p finish p id p payload end Controller respond section class PostsController < ApplicationController # GET /posts # GET /posts.json respond_to :json, :html …. end 控制器创建动作 def create @post = Post.new(params[:post]) @post.save! respond_with(@post, :location => nil) end 控制台输出 […]

无法使用cache_classes = true为Concern(ActiveSupport :: Concern :: MultipleIncludedBlocks)定义多个“包含”块

我有一个在Rails 4.1.1应用程序中使用的模块 module A extend ActiveSupport::Concern included do #Some code end end 它包含在一个类中 class Some include A end 这适用于application.rb cache_classes=true 。 现在,如果我关闭类的缓存,我得到Cannot define multiple ‘included’ blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)启动服务器Cannot define multiple ‘included’ blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)exceptionupson Cannot define multiple ‘included’ blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks) 。 由于重新加载类是由Rails完成的,应该如何处理这样的问题?

长期无安全方法链

所以我知道一些我所知道的不同方法,我想探索各种方法的优缺点,这些方法有以下几种: 可读性 性能 易于调试 OO原则(低耦合和高内聚) 从主动支持中明确使用try方法 person.try(:pet).try(:name).try(:upcase) 使用救援无 person.pet.name.upcase rescue nil 使用&&运算符链 person && person.pet && person.pet.name && person.pet.name.upcase 猴子修补Object类,请参阅https://gist.github.com/thegrubbsian/3499234获取原始要点 class Object def try_all(*methods) values = [self] methods.each do |method| value = values.last.try(method) return nil if value.nil? values << value end values.last end end person.try_all(:pet, :name, :upcase) 不要使用nil安全代码,而是在调用代码之前validation数据 #not a good implementation by any means […]

Rails:在时区中获取#beginning_of_day

我有一个rails应用程序的默认时区设置。 和Date对象的一个​​实例。 如何让Date#beginning_of_day返回指定时区内的一天的开始,而不是我当地的时区。 是否有其他方法可以在给定日期的指定时区内开始白天时间? date = Date.new(2014,10,29) zone = ActiveSupport::TimeZone.new(‘CET’) date.foo(zone) # should return “Wed, 29 Oct 2014 00:00:00 CET +01:00” zone = ActiveSupport::TimeZone.new(‘UTC’) date.foo(zone) # should return “Wed, 29 Oct 2014 00:00:00 UTC +00:00”

如何将TZInfo标识符转换为Rails TimeZone名称/密钥

如何将作为TZInfo标识符接收的js值转换为Rails TimeZone名称/密钥? FROM: “America/New_York”从JavaScript TZinfo检测返回 TO:Rails TimeZone中使用的”Eastern Time (US & Canada)”惯例 或者另一个例子: “Pacific/Honolulu” =>转换为=> “Hawaii” 两者都可以在ActiveSupport::TimeZone < Object mapping中使用,但rails在下拉,validation和存储到Time.use_zone()使用密钥[ig “Eastern Time (US & Canada)” ]。 基于我对ActiveSupport::TimeZone.us_zones理解,这似乎很重要,尤其是DayLights节省时间(轨道声音可以很好地处理),并且只能匹配偏移量。 如果没有使用rails TimeZone名称存储到DB,则validation失败并且在用户的配置文件设置页面中与ActiveSupport::TimeZone.zones_map的下拉列表无法正确匹配 这样做的目的是用户不必在注册时选择他们的时区,或者在注册后需要在他们的设置中更改它们。 浏览器检测到它并在注册时将其传递给hidden_​​field。 在极少数情况下,他们在与家庭/工作不同的地方注册。 他们可以稍后在其帐户设置中手动覆盖。 在尝试摄取js时区检测时似乎是一个常见的差距。 这甚至可能成为如何将返回的信息从js传递给rails进行转换然后返回js以存储回表单的hidden_​​field的次要问题? 希望我正确地解决了这个问题,并且确实有一些带有rails的绿色,所以可能有一个简单的解决方案…… 非常感谢所有的帮助! -E ActiveSupport Time.zone文档 http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-i-parse MAPPING = {“Eastern Time (US & Canada)” => “America/New_York” 使用js打包gem’temporal-rails’来检测用户时区: https://github.com/jejacks0n/temporal 用户Time_Zone实现如下所示: http://railscasts.com/episodes/106-time-zones-revised *使用设计和设计 – […]

Bundler找不到gem“activesupport”的兼容版本

我迁移到3.2.13版本的rails并且我收到此错误,我该如何摆脱这个? Fetching gem metadata from https://rubygems.org/……. Fetching gem metadata from https://rubygems.org/.. Resolving dependencies… Bundler could not find compatible versions for gem “activesupport”: In snapshot (Gemfile.lock): activesupport (3.2.12) In Gemfile: rails (= 3.2.13) ruby depends on activesupport (= 3.2.13) ruby Running `bundle update` will rebuild your snapshot from scratch, using only the gems in your Gemfile, […]