Mongoid定制的setter / getters和super

我正在尝试修改属性Mongoid模型上的setter,但与ActiveRecord不同,我不能调用super来让Mongoid实际设置属性,因为模型使用include Mongoid::Document而不是ActiveRecord::Base的子类。 我希望能够做这样的事情。 class User include Mongoid::Document embeds_one :email_account def email_account=(_email_account) ret = super puts “email account updated!” do_something ret end end 除了,因为它不是一个子类,产生 NoMethodError: super: no superclass method 想法? 编辑: 你会怎么做一个吸气剂,比如 class User include Mongoid::Document embeds_one :email_address def email_address super || “myself@gmail.com” end end

使用正则表达式检查最少8位的字符串

我有正则表达式如下: /^(\d|-|\(|\)|\+|\s){12,}$/ 这将允许数字,(,),空格。 但我想确保字符串包含至少8位数字。 一些允许的字符串如下: (1323 ++24)233 24243434 43 ++++43435++4554345 434 它不应该允许字符串: ((((((1213))) ++++232+++

Paperclip不支持.doc文件

在rails 4.0.2中,我使用paperclip gem上传文件。 但它不支持.doc文件。 在文件上传字段下方,它显示错误消息“具有与其内容不匹配的扩展名” 在模型中,检查内容类型的validation如下: validates_attachment_content_type :document, :content_type => [‘application/txt’, ‘text/plain’, ‘application/pdf’, ‘application/msword’, ‘application/vnd.openxmlformats-officedocument.wordprocessingml.document’, ‘application/vnd.oasis.opendocument.text’, ‘application/x-vnd.oasis.opendocument.text’, ‘application/rtf’, ‘application/x-rtf’, ‘text/rtf’, ‘text/richtext’, ‘application/doc’, ‘application/docx’, ‘application/x-soffice’, ‘application/octet-stream’] 现在使用的gem rails (4.0.2, 4.0.0, 3.2.13, 3.2.8, 3.0.4, 3.0.3) paperclip (3.5.2, 2.3.11, 2.3.8) 我该如何解决这个问题?

Rspec测试失败,当用户被授权访问他拥有的交易时,用户被重定向

在我的应用程序中,感谢Cancan,我可以让客户访问他自己的交易。 当我使用浏览器“手动”尝试它时,它可以正常工作, 但我在实现rspec测试时失败了 。 客户无法访问其他客户的交易,只能访问自己的交易(管理员通过Active Admin界面授予他访问权限)。 这就像我没有让rspec明白我为测试创建的客户(通过FactoryGirl)应该被允许/与我为测试创建的交易相关联(再次通过FactoryGirl)。 测试:/spec/controllers/deals_controller_spec.rb require ‘spec_helper’ require “cancan/matchers” describe DealsController do context “As signed-in CUSTOMER” do before do @customer = FactoryGirl.create(:customer) #the factory builds a basic customer ie with ‘prospect role’ attributed by default @deal = FactoryGirl.create(:deal, :customers => [@customer]) # in array as a deal has_many customers sign_in_customer @customer end […]

Ruby:使用group方法时,Prawn PDF内存不足

我想要以PDF格式显示大量产品,并带有类别标题。 如果某个类别不适合当前页面,我想将其移至下一页。 为此,我正在使用Prawn的组方法。 Product.all.group_by(&:category).each do |category, products| pdf.group do # Simplified the data a bit for this example pdf.text category pdf.table products.map{ |p| [p.title, p.price] } end end 这适用于少量产品,但是当我添加超过100左右时,它需要很长时间,然后以“未能分配内存”结束。 如果我不使用组方法,则需要大约30秒。 显然,组方法不能很好地管理其内存使用。 任何建议的解决方法将不胜感激。

validation嵌套表单中子对象的唯一性无法正常工作

我在Rails中的作用域唯一性validation有问题。 如果我尝试使用我不想重复的相同属性集创建一个新对象,直接在子模型中,但是当我尝试创建一个具有两个不唯一的子节点的父对象时,它可以正常工作没有被触发。 背景 我在Rails 3.2中有一个应用程序,其在HAML中的视图使用simple_form。 我有两个模型: Page和Property 。 页面可以具有许多属性,并且它接受属性的嵌套属性。 我想validation一个页面不能有两个具有相同名称的属性: #app/models/page.rb class Page :true end #app/models/property.rb class Property VALID_PROPERTIES, :uniqueness => {:scope => :page_id} end 当然,该属性具有page_id属性。 就像我说的,当通过其表单创建新属性时,validation工作。 如果我尝试创建一个具有相同名称和相同page_id的新属性,Rails会告诉我该名称已被删除。 问题 如果我创建一个新页面,并通过嵌套表单,分配各种属性,我可以绕过此validation。 当数据库中尚未存在page_id和property_id的组合时,这似乎只是一个问题,例如,如果我编辑已保存属性的Page模型,我尝试添加一个新的一个同名,validation现在触发。

工头与puppet node.rb错误404未找到

我已经安装了带有木偶的foreman-1.2 ,安装完成后我已将我的木偶注册到工smart-proxy 。 当我运行以下命令 [root@puppet ~]# puppet agent -t Warning: Unable to fetch my node definition, but the agent run will continue: Warning: Error 400 on SERVER: Failed to find puppet.example.com via exec: Execution of ‘/etc/puppet/node.rb puppet.example.com’ returned 1: — false Info: Retrieving plugin Error: Could not retrieve catalog from remote server: Error 400 on […]

MiniTest中的方法期望

我正在尝试为ActiveRecord编写一个测试 – 而Rails使用MiniTest进行测试,所以我没有选择测试框架。 我想测试的条件是这个(来自db:创建rake任务,为了这个例子的目的拉入一个方法): def create_db if File.exist?(config[‘database’]) $stderr.puts “#{config[‘database’]} already exists” end end 所以,我想测试$ stderr如果File存在则收到puts,否则不会。 在RSpec中,我会这样做: File.stub :exist? => true $stderr.should_receive(:puts).with(“my-db already exists”) create_db MiniTest中的等价物是什么? assert_send似乎没有像我期望的那样行事(并且那里没有任何文档 – 它应该在执行之前运行,比如should_receive,还是之后?)。 我想我可以在测试期间暂时使用模拟设置$ stderr,但$ stderr只接受响应write的对象。 你不能在模拟上存根方法,我不想在我的stderr模拟上设置write方法的期望 – 这意味着我正在测试一个我正在嘲笑的对象。 我觉得我没有在这里使用MiniTest正确的方式,所以一些指导将不胜感激。 更新:这是一个有效的解决方案,但它正在设置期望:write,这是不对的。 def test_db_create_when_file_exists error_io = MiniTest::Mock.new error_io.expect(:write, true) error_io.expect(:puts, nil, [“#{@database} already exists”]) File.stubs(:exist?).returns(true) original_error_io, $stderr = $stderr, error_io […]

我如何在Ruby中使用RAW套接字?

我正在尝试使用Ruby创建一个原始套接字。 问题是,那里没有任何称为“原始套接字”的东西,另一方面,Socket类本身并没有完全记录。 有没有人在Ruby中为那种套接字提供一些代码示例,或者可能是某种类型的文档? 顺便说一句,我已经知道如何使用TCPSocket和TCPServer类,我需要的是特别是原始套接字。

Rails收集JSON数据并将其呈现到Highcharts图中

我试图将标签传递到x轴上的Highcharts柱形图。 目前图表的工作原理是它渲染数据,但x轴标签显示0,1,2,3 …等,这是没有任何渲染时的默认值。 notes_controller: def dashboard @data = Note.getData() end note.rb def self.getData data = [] self.subject_types.each do |type| data << self.type_count(type) end data end private def self.subject_types pluck(:subject_type).uniq end def self.type_count(type) where(subject_type: type).count end end dashboard.html.erb javascript …series: [{ name: ‘Number of Notes By Class Module’, data: }]… 以上作品很棒,但我如何才能展示标签? 它们应该来自我的表中名为subject_type的列。 我尝试过以下方法: note.rb: def self.getSubjects […]