草庐IT

add_by_value

全部标签

ruby-on-rails - rails 4 : Forcing specific values with "strong parameters"

我正在努力使用用于创建“项目”的表单。最后,我只想允许当前的user_id,但我还没有构建用户模型或身份验证,因此作为占位符,我只是硬编码1。app/views/projects/new.html.erb:{:action=>'create'})do|f|%>1})%>app/controllers/projects_controller.rbdefcreate#saveobjectifProject.create(project_params)redirect_to(:action=>'show')elserender('new')endendprivatedefproject_pa

ruby-on-rails - rails/ ruby : TimeWithZone comparison inexplicably failing for equivalent values

我在当前项目中比较DateTime时遇到了一段糟糕的时光(没有双关语意),特别是比较ActiveSupport::TimeWithZone的两个实例。问题是我的两个TimeWithZone实例具有相同的值,但所有比较都表明它们不同。执行调试时暂停(使用RubyMine),可以看到如下信息:timestamp={ActiveSupport::TimeWithZone}2014-08-0110:33:36UTCstarted_at={ActiveSupport::TimeWithZone}2014-08-0110:33:36UTCtimestamp.inspect="Fri,01Aug20

ruby - 背包 : how to add item type to existing solution

我一直在使用动态规划的这种变体来解决背包问题:KnapsackItem=Struct.new(:name,:cost,:value)KnapsackProblem=Struct.new(:items,:max_cost)defdynamic_programming_knapsack(problem)num_items=problem.items.sizeitems=problem.itemsmax_cost=problem.max_costcost_matrix=zeros(num_items,max_cost+1)num_items.timesdo|i|(max_cost+1).ti

ruby - `sort_by' : comparison of Array with Array failed (no nil data)

classCustomSorterattr_accessor:start_date,:availabledefinitialize(start_date,available)@start_date=Time.mktime(*start_date.split('-'))@available=availableendendcs1=CustomSorter.new('2015-08-01',2)cs2=CustomSorter.new('2015-08-02',1)cs3=CustomSorter.new('2016-01-01',1)cs4=CustomSorter.new('2015-0

ruby-on-rails - 点击 Google Contacts API 时出现 "connection reset by peer"错误

我正在尝试使用GoogleContactsAPI将GoogleContacts拉入Rails应用程序。我已经完成了Oauth2握手,现在正在使用我的访问token请求protected资源。这是代码:uri=URI('https://www.google.com/m8/feeds/contacts/default/full')params={:client_id=>APP_CONFIG[:google_api_client_id],:access_token=>auth.access_token,"max-results".to_sym=>max_results}uri.query=U

ruby-on-rails - 为什么 PG::UniqueViolation: ERROR: duplicate key value violates unique constraint?

我有一个模型Post,每次创建帖子时,我都希望同时创建一个新的Moderation实例。所以在post.rb中我使用回调after_save:create_moderation然后写一个私有(private)方法:...includeReportableafter_save:create_moderationprivatedefcreate_moderationself.create_moderation!(blog:Blog.first)end但是在创建提案时出现此错误:PG::UniqueViolation:ERROR:duplicatekeyvalueviolatesunique

arrays - Enumerable 的 group_by 是否保留 Enumerable 的顺序?

是否Enumerable#group_by保留每个值内的原始顺序?当我得到这个时:[1,2,3,4,5].group_by{|i|i%2}#=>{1=>[1,3,5],0=>[2,4]}是否保证,例如,数组[1,3,5]包含此顺序的元素,而不是,例如[3,1,5]?有没有关于这一点的说明?我没有提到键1和0之间的顺序。那是一个不同的问题。 最佳答案 是的,Enumerable#group_by保留输入顺序。这是该方法在MRI中的实现,来自https://github.com/ruby/ruby/blob/trunk/enum.c:s

ruby-on-rails - find_or_initialize_by 不适用于 2 列

我正在尝试使用ActiveRecord在我的数据库中插入数据。当我使用pdays=MyModel.new而不是下面的find_or_initialize_by进行初始化时,脚本运行良好。但它只运行一次。我需要它每天运行以执行更新。当我第二次尝试使用pdays=MyModel.new运行脚本时,没有出现重复键约束。因此,我正在尝试使用以下带有2个参数的find_or_initialize_by,但这给出了一个错误:undefinedmethod`find_or_initialize_by'2列一起构成唯一记录:vertica_traffic.eachdo|vdays|pdays=MyMo

ruby-on-rails - rails : How do I check if a column has a value?

我怎样才能做到这一点?...Cell:...我想测试代理是否有手机号码,如果有,显示条件中的内容。我目前拥有的似乎不起作用;它只显示“单元格:”。想法? 最佳答案 这是你要求的:Cell:细胞?无论cell是nil还是空字符串,方法都有效。Rails为所有ActiveRecord属性添加了类似的功能。这看起来会好一点:Cell:问号和冒号构成了一个快速的“if?then:else”语句。上面代码中有两个问号是因为一个是方法名单元格的一部分?另一个是if/then/else构造的一部分。

ruby sort_by 两次

Ruby在Enumerables上有一个sort_by方法。极好的!所以你可以做类似的事情entries.sort_by{|l|l.project.name}这将按项目名称对一堆条目进行排序。您如何使用它,以便在具有相同名称的项目中,条目按时间排序? 最佳答案 我建议将您要排序的列放入一个数组中。entries.sort_by{|l|[l.project.name,l.project.time]}这将遵循每种类型的自然排序顺序。 关于rubysort_by两次,我们在StackOverf