这是一个数组示例:
{"C1"=>[
{:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"},
{:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>"$21", :color=>"black", :size=>"S", :description=>"descr"}, # ...
],
#...
}
Run Code Online (Sandbox Code Playgroud)
在这里,当我试图从该数组中获取数据时:
@array.each do |p|
product = Product.new
product.sku = p[0]
product.name = p[1][0][:name] #can't convert Symbol into Integer
price = p[1].select{ |pr| !pr[:price].nil? and pr[:price] != "0" }.min_by{ |i| i[:price].to_f }[:price]
product.price = "%.2f" % (price.to_f)
...
end
Run Code Online (Sandbox Code Playgroud)
每次我尝试从数组中获取数据时,我都会遇到product.name =错误无法将 Symbol 转换为 Integer。
在这种情况下有什么问题?我在这个问题上花了一个下午的时间,但不幸的是我仍然无法弄清楚......
谢谢你
我有以下型号:
class Product < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :recommendation_sets, :through => :product_recommendation_sets
end
class ProductRecommendationSet < ActiveRecord::Base
belongs_to :product
belongs_to :recommendation_set
end
class RecommendationSet < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :products, :through => :product_recommendation_sets
has_many :recommendation_recommendation_sets, :dependent => :destroy
has_many :recommendations, :through => :recommendation_recommendation_sets
end
class RecommendationRecommendationSet < ActiveRecord::Base
belongs_to :recommendation
belongs_to :recommendation_set
end
class Recommendation < ActiveRecord::Base
has_many :recommendation_recommendation_sets, :dependent => :destroy
has_many :recommendations, :through => :recommendation_recommendation_sets
end
Run Code Online (Sandbox Code Playgroud)
我试图选择所有recommendationsproduct_id = x,通过这样做: …
我有一个名为"products"的数据库,它有一个使用hstore调用的字段data.
目前,在我的index.html.haml中,我只是循环浏览产品并将其显示data为哈希:
- @products.each do |product|
=product.data #THIS PRINTS A HASH
%hr
Run Code Online (Sandbox Code Playgroud)
例如,哪个可以打印哈希,如:
{"Name"=>"Example","type"=>"book", "price"=>"7.99"}
Run Code Online (Sandbox Code Playgroud)
我想创建一个可以保存动态数量的键和值的HTML表,并将它们打印到具有与键对应的值的列中.这是一个图表:

谢谢大家的帮助!
我创建了一个迭代Order对象数组的类方法.我正在使用那里的数据来构建哈希.迭代中的一个if块是:
if !(report_hash[user_id][reason])
report_hash[user_id][reason] = 1
else
report_hash[user_id][reason]++
end
Run Code Online (Sandbox Code Playgroud)
当我运行这个方法时,我得到:
.rb:66 syntax error, unexpected keyword_end (SyntaxError)
Run Code Online (Sandbox Code Playgroud)
66号线是end生命的所在.为什么Ruby不期望在这个块的末尾有一个结束语句?一旦一切正常,我打算将所有条件逻辑移动到单独的类方法中,但我一直试图解决这个问题并且有点卡住了.
我的会议模型有一个名为meeting_date的dateTime字段.我想以这样的方式在.where(...)查询中使用它:
<% @meetings = Meeting.where(meeting_date.to_date == Date.today.to_s %>
Run Code Online (Sandbox Code Playgroud)
怎么能实现这一目标?
可能是一个愚蠢的问题...我是ruby的新手,最近我正在编写一个rake任务来将多个表合并为一个通用的表.我需要做的一件事是从数据库中获取日期,然后将日期转换为两个整数作为年份和月份,然后将它们保存到两个单独的列中.
我上周完成了这个任务文件,但不幸的是,该文件被意外删除,所以我必须再次编写代码.我不记得我是如何操纵原始文件中的日期的,我认为我在原始文件中采用的方式比当前代码更直接.目前的代码如下.
fetched_time=DateTime.strptime(pr.fetched_time,"%Y-%m-%d")
dr.year = fetched_time.strftime('%Y').to_i
dr.month = fetched_time.strftime('%m').to_i
Run Code Online (Sandbox Code Playgroud)
我尝试过很多关键词来搜索,但结果都没有用.以下代码是将日期字符串转换为整数的最佳方法吗?
非常感谢你.
这种情况还没有问题。我不想检测字符串是否包含任何链接,即 http、ftp、https 和 www. 等的所有组合
基本上我想防止字符串包含任何链接。
我目前使用:
name.split(/\s+/).find_all { |u| u =~ /^https?:/ }).count
Run Code Online (Sandbox Code Playgroud)
防止字符串中的任何链接的最佳方法是什么?
在 Rails 中,我试图本地化日期:
2.1.1 :005 > Date.today
=> Mon, 14 Apr 2014
2.1.1 :006 > I18n.localize(Date.today)
=> "14/04/2014"
2.1.1 :007 >
Run Code Online (Sandbox Code Playgroud)
第二个输出不是第一个的正确翻译!
你能帮助我吗 ?
我有一个字符串数组,这是多选复选框的结果,当复选框中没有选项时,数组不会改变,结果是最后一个数组保存!当复选框没有选择值时,我想显示类似""的内容.
<%= f.label :disposizione_campionamento,"Disposizione" %>
Random <%= f.check_box :disposizione_campionamento, { :multiple => true },
"Random", nil %>
Sistematica <%= f.check_box :disposizione_campionamento,{ :multiple => true },
"Sistematica", nil %>
Stratificata <%= f.check_box :disposizione_campionamento,{ :multiple => true },
"Stratificata",nil %><br/>
Run Code Online (Sandbox Code Playgroud)
在模型中
serialize :disposizione_campionamento, Array
Run Code Online (Sandbox Code Playgroud)
如果我检查Random,Sistematica和Stratificata,结果是"Random,Sistematica,Stratificata",但是如果我修改数组,取消选中所有三个值,结果就是"Random,Sistematica,Stratificata"
当我提交表格时,我在控制器中有这个:
def create
modulo2 = Modulo2.find(params[:modulo2_id])
@variabili = modulo2.variabilis.create(params[:id])
respond_to do |format|
if @variabili.save
format.html { redirect_to(modulo2_variabilis_path, :notice =>
'Modifica effettuata') }
format.xml { render :xml => @variabili, :status => :created, :location =>
[@variabili.modulo2, @variabili] }
else …Run Code Online (Sandbox Code Playgroud) 关于这个主题肯定已经有人问过类似的问题,但我找不到它,也找不到合适的词来寻找它。
假设我有这个:
a -- b <-- Master
\
d -- e <-- Branch1
\
f -- g <-- Branch2
Run Code Online (Sandbox Code Playgroud)
d但在某些时候,在合并到 Master 之前,我必须重写 commits和eof中完成的代码Branch1,所以它们变成了d'和e':
a -- b <-- Master
|\
| d'--e' <-- Branch1
\
d --e
\
f -- g < -- Branch2
Run Code Online (Sandbox Code Playgroud)
我如何告诉我的Branch2父f提交应该是e'而不是旧的e?
a -- b <-- Master
\
d'-- e' <-- Branch1
\
f -- g <-- Branch2
Run Code Online (Sandbox Code Playgroud)
基本上,当Pull Request通过 Github …