这个问题源于:如何在创建rails join表之后链接表单
我正在创建产品和类别模型之间的连接表.
连接表应该命名为什么?categories_products或category_products或其他什么?
ruby-on-rails rails-migrations ruby-on-rails-3 ruby-on-rails-3.1
我的标签:
<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5], :selected => :option ])) %>
Run Code Online (Sandbox Code Playgroud)
如何将所选值设置为选择的选项.例如,如果我选择['Bought', 3]并提交,['All', 1]则选择选项.如何在提交表单后显示所选值.
当我尝试安装我的Cron作业时,我收到以下错误:
"crontab.txt":8:crontab文件中的EOF错误,无法安装
我试图将数据从HBase Shell导出到我可以解析的文本文件,并添加到msysql数据库.
我目前正在使用以下命令:
echo "scan 'registration',{COLUMNS=>'registration:status'}" | hbase shell > registration.txt
Run Code Online (Sandbox Code Playgroud)
它导出从hbase shell到registration.txt的所有内容.
如何删除shell简介和摘要,只需将数据行附加到文本文件中:
例如:Shell我想省略:
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.5-mapr, Wed May 1 7:42:07 PDT 2013
Run Code Online (Sandbox Code Playgroud)
总结我想省略:
ROW COLUMN+CELL
4419 row(s) in 12.9840 seconds
Run Code Online (Sandbox Code Playgroud) 我试图找出为什么我invalid date在某些日期不断收到错误.
例如:
这有效:
e = "07/02/2013"
=> "07/02/2013"
start_date = DateTime.parse(e).beginning_of_day.strftime("%Y-%d-%m %H:%M:%S")
=> "2013-07-01 00:00:00"
end_date = DateTime.parse(e).end_of_day.strftime("%Y-%d-%m %H:%M:%S")
=> "2013-07-02 23:59:59"
Run Code Online (Sandbox Code Playgroud)
这回来了 ArgumentError: invalid date
e = "07/18/2013"
=> "07/18/2013"
start_date = DateTime.parse(e).beginning_of_day.strftime("%Y-%d-%m %H:%M:%S")
ArgumentError: invalid date
from (irb):53:in `parse'
end_date = DateTime.parse(e).end_of_day.strftime("%Y-%d-%m %H:%M:%S")
ArgumentError: invalid date
from (irb):55:in `parse'
Run Code Online (Sandbox Code Playgroud)
我在两种情况下都使用相同的日期格式.可能是什么原因/我需要更改什么来修复它?
如果找到记录,Rails find_or_create_by()是否也更新记录属性.如果没有,是否有一种简单的方法来find_or_create一条记录,如果该记录已经存在; 更新记录属性
ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2
我有以下型号:
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,通过这样做: …
假设我有控制器X继承自控制器Y:
class X < YController
Run Code Online (Sandbox Code Playgroud)
我的YController继承自ApplicationController.
我是否仍然可以通过ApplicationController中提供的X控制器访问所有方法/类?
我有一种方法可以计算给定记录集的平均值:
input = params[:recommendation_ratings].values # The params are sent from radio_tags in my view.
input.each do |mini_params|
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id) #Find record on Rating table.
l.rating #Get value associated with rating_id
total_rating = []
total_rating << l.rating
average = total_rating.inject{ |sum, el| sum + el }.to_f / total_rating.size
puts average
end
Run Code Online (Sandbox Code Playgroud)
l.rating没有附加到total_rating数组中。看跌期权平均数打印为:
3.0
3.0
3.0
3.0
3.0
Run Code Online (Sandbox Code Playgroud)
如何将返回到数组的每个评级附加到计算平均值和其他数学函数。
如何在生产中永久更改我的应用程序端口号?
我知道在启动服务器时,我可以指定端口号rails s -p 3005,但不知道如何改变它,这样我不必specifiy端口号,每次我启动的应用程序.
我的模型中有一个方法可以解析 API 并返回数据:
def self.get_item_info(date, time, limit)
url = URI.parse("#{end_point_url}/#{date}?mRange=#{time}&limit=#{limit}")
@response = JSON.parse(Net::HTTP.get_response(url).body)
if @response["ERROR"]
flash[:notice] = { :notice => "Sorry, No Data returned. Message: #{@response["ERROR"]["errorMessage"]}" }
else
@data = []
@items = @response["v1"]["items"]
@items.each do |item|
add_data(item["itemNbr"])
@data << [@fam_id = item["FamId"],
@item_nbr = item["itemNbr"],
@item_desc = item["itemDesc"]]
end
return @data
end
end
Run Code Online (Sandbox Code Playgroud)
我的控制器:
def index
if
date = params[:date]
time = params[:time]
limit = params[:limit].to_i
Product.get_item_info(date, time, limit)
if @message
redirect_to "/view_api", :flash => { :notice => "Sorry, …Run Code Online (Sandbox Code Playgroud)