草庐IT

fields_to_export

全部标签

ruby-on-rails - rails : how to extend a gem's ActiveRecord child class?

我在扩展一个在gem中定义并且是ActiveRecord::Base的子类的类时遇到问题。我唯一想扩展这个类的是:有很多:promos然而,扩展倾向于排除原始类。我得到的错误:PGError:ERROR:relation"sites"doesnotexistLINE4:WHEREa.attrelid='"sites"'::regclass^:SELECTa.attname,format_type(a.atttypid,a.atttypmod),d.adsrc,a.attnotnullFROMpg_attributeaLEFTJOINpg_attrdefdONa.attrelid=d.a

ruby-on-rails - 在 Rails 中,为什么 1.year.from_now 与 1.year.to_i.seconds.from_now 不同?

我能否让Rails在几秒钟内将相同的逻辑应用于我的计算,就像它在几年内对我的计算所做的一样?puts"#{1.year.from_now}|#{1.year.to_i.seconds.from_now}"2017-03-2318:48:06UTC|2017-03-2400:48:06UTC我不明白这6小时的时差从何而来。 最佳答案 相差6小时。这是因为以秒为单位的1年(由to_i方法转换)在RubyonRails核心扩展中被定义为365.25天:>>1.year.to_i/60/60/24.0=>365.25这0.25天是实际的6小

ruby - 为什么需要 mongo 给我 LoadError : no such file to load -- openssl

我正在使用UbuntuServer10,Ruby1.9.2当我尝试要求'mongo'时它给我这个错误:irb(main):001:0>require'mongo'LoadError:nosuchfiletoload--opensslfrom/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in`require'from/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in`require'from/usr/local/lib

ruby-on-rails - rails : Human-friendly date format for text_field

我有一个存储为日期时间值的birth_date字段。默认的railsformhelpers吐出一种不太友好的格式,例如“2008-06-1022:33:19.000000”。下面是Vanillarails方式。"20"%>我怎样才能简单地应用一种格式?我尝试了各种方法,例如strftime应该可以,我想。但是当我尝试以下操作时,出现错误undefinedmethod'strftime'fornil:NilClassf.object.birth_date.strftime('%m/%d/%Y'),:size=>"20"%>根据其他一些问题/答案,我尝试了以下方法。它适用于非空值,但它是丑

ruby-on-rails - ActiveRecord 查询 : where a field is not true

我想查找某个字段不正确的所有记录。用于此的有效AR语法是:Dog.where(:stray=>[false,nil])是否有更简洁的方式来查询“nottrue”?到处都必须迎合这种mysql的细微差别,这真的很糟糕。 最佳答案 命名作用域怎么样?scope:not_stray,where("strayISNULLORstray=false")然后使用:Dog.not_stray 关于ruby-on-rails-ActiveRecord查询:whereafieldisnottrue,我们在

ruby-on-rails - Rails ActiveRecord - update_all : how to update multiple fields at once with mixed type of arguments

我有一个用例,我想使用ActiveRecord::Relationupdate_all方法并指定要设置的几个字段。我使用update_all是因为可以更新很多条目,我不想将它们全部加载并一个一个地更新。其中一些需要直接的SQLSET语句,例如因为我根据另一列的值设置一列。是否有一个简单的语法与update_all一起使它可读,沿着这个=>MyModel.where(state::foo).update_all(['timespent=timespent+500',#Can'tbeanythingelsethanaSQLstatementstate::bar,#RegularRailsS

ruby-on-rails - Ruby to_proc 破解与绑定(bind)

我正在尝试创建一个小的Rubyhack来制作类似于Symbol#to_prochack的反向操作。而Symbol#to_prochack使这成为可能:some_array.each(&:some_method)与相同some_array.each{|obj|obj.some_method}我想让这成为可能:some_array.each(&[:some_method])会和一样some_array.each{|obj|some_method(obj)}问题在于,除非some_method是内核方法,否则它的真正含义是:some_array.each{|obj|self.some_met

ruby-on-rails - 如何在 json 渲染中获取完整的 belongs_to 对象?

基本上,我有一个属于:companies的对象,并且具有:company_id属性。当我呈现json:@coupons时,JSON是否可能包含其所有者的属性而不是company_id? 最佳答案 你也许可以做一些类似render:json=>@coupons.to_json(:include=>:company)的事情,至少它似乎在我最初的rails2.3测试中有效.8.答案已编辑为使用:include=>:company而不是:include=>:companies 关于ruby-on

ruby-on-rails - 回形针 gem - "Image has contents that are not what they are reported to be"错误

该网站的功能是发布博客文章。它在Windows7上本地运行。我试过Paperclipgem(版本4.2.4和4.3)并且服务器在cmd中进入无限循环(在4.2.4上没有发生但仍然出现错误)。我做了bundleinstall并且它确实安装了。gem文件:gem"paperclip","~>4.3"这是模型:classPost":style/rails1.jpg"validates_attachment_content_type:image,:content_type=>/\Aimage\/.*\Z/end这是我在尝试提交图像(png或jpg)时遇到的错误:Imagehascontents

ruby-on-rails - Rails - ActionView::Base.field_error_proc 向上移动 DOM 树?

有没有办法从传入的html_tag元素向上攀登DOM树?ActionView::Base.field_error_proc=Proc.newdo|html_tag,instance|#implementationend无论如何我可以实现这个方法来向上移动DOM树并将一个类放在父div上吗?例如:EmailAddress我想在div.email上放置一个类,而不是直接在输入/标签上放置一些东西。这可以用field_error_proc方法完成还是有一个干净的替代方法?我想避免在我对每个表单字段的View中明确地这样做。(像下面这样).email{:class=>object.errors