我试图为给定用户的每日购买金额加总.@dates是一个包含31个日期的数组.我需要find条件来比较数组中的日期和购买的created_at日期.我在下面做的比较create_at列的确切DateTime.我需要它来看一天本身,而不是DateTime.
我怎么写这个,所以created_at介于数组的日期之间?
<% @dates.each do |date| %>
<td><%= current_user.purchases.sum(:amount, :conditions => ["created_at = ?", date]) %></td>
<% end %>
Run Code Online (Sandbox Code Playgroud) 我有一个ruby on rails application.I有2个下拉框.我必须根据第一个选择填充第二个下拉框.
HTML代码是
-Releases = Release.all
%table.grid.full
%tr
%td.grid.full_panels{:style => "width: 40%"}
Release:
%td.grid.full_panels{:style => "width: 40%"}
= select_tag "releases",options_from_collection_for_select(releases,"id","name",params[:releases]),:include_blank=>true
%td.grid.full_panels{:style => "width: 40%"}
Cycle:
%td.grid.full_panels{:style => "width: 40%"}
Run Code Online (Sandbox Code Playgroud)
现在我需要循环下拉以从发布中填充.
请帮我解决这个问题.
我在当前项目中使用gmaps4rails非常棒的gem,并且必须在DB中导入超过一千个应该充当gmappable的对象.
然而,并非所有人都有正确的地址!因此,当我尝试导入时,在第一个无法使用Gmaps4rails :: GeocodeStatus进行地理编码的地址上失败.
是否有可能在可能的情况下跳过错误和地理编码?
我想出了这个:
acts_as_gmappable :process_geocoding => false
before_save :prepare_gmaps
private
def prepare_gmaps
begin
data = Gmaps4rails.geocode(address).first
self.latitude= data[:lat]
self.longitude= data[:lng]
rescue Gmaps4rails::GeocodeStatus
end
end
Run Code Online (Sandbox Code Playgroud) validation geocoding ruby-on-rails ruby-on-rails-3 gmaps4rails
给定一个带有日期名称的字符串,即"Wednesday"如何在当前周(周一至周日)内获得表示该日的Time对象?
因此,例如,如果当前日期是5月29日星期二,并且字符串是"Monday",则时间对象应该表示Monday 28 May.如果字符串是"Friday",它应该是Friday 1 June.
如果当前日期是6月5日星期二,而字符串是"Monday",则时间对象应该代表Monday 4 June.如果字符串是"Friday",它应该是Friday 8 June.
我使用的是Rails 3.2.0和Ruby 1.9.2.
主页正常打开,但当我尝试导航到我网站上的其他页面时,我收到404错误.但是,当我将index.php放入网址时,页面确实正常打开.
在我的配置文件中,我已设置$config['uri_protocol'] = "AUTO";.
我有以下内容:
@products = {
2 => [
#<Review id: 9, answer01: 3, score: 67, style_id: 2, consumer_id: 2,
branch_id: 2, business_id: 2>
],
15 => [
#<Review id: 10, answer01: 3, score: 67, style_id: 2, consumer_id: 2,
branch_id: 2, business_id: 2>,
#<Review id: 11, answer01: 3, score: 67, style_id: 2, consumer_id: 2,
branch_id: 2, business_id: 2>
]
}
Run Code Online (Sandbox Code Playgroud)
我想平均与每个产品的哈希键相关的所有评论的分数.我怎样才能做到这一点?
我想制作一个条件语句来检查是否:
comment.user.name
Run Code Online (Sandbox Code Playgroud)
(可能返回类似的内容"Montgomery Philips Ridiculouslylonglastname")包含任何超过15个字符的单词.
就像是:
if comment.user.name.split(" ").???
Run Code Online (Sandbox Code Playgroud) 我正在努力进行迁移,但我遇到了一些麻烦.当我尝试在Heroku上运行"rake db:migrate"时,我收到了一条错误消息.现在我在我的localhost中发现迁移中的代码有问题 - 但我不知道是什么.
这是我的迁移代码:
def change
add_column :comments, :likes_count, :integer, :default => 0
Comment.all().each do |comment|
comment.update_attribute(:likes_count, comment.likes.count)
comment.save
end
end
Run Code Online (Sandbox Code Playgroud)
这是我在控制台上出现的错误(当我尝试在"rails console"上复制并粘贴此代码时):
SyntaxError: (irb):3: syntax error, unexpected ',', expecting ')'
c.update_attribute (:likes_count, comment.likes.count)
^
(irb):3: syntax error, unexpected ')', expecting keyword_end
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
---------------------编辑---------------------------
The奇怪的是:我已经在localhost上运行了这个迁移,并且localhost中的所有东西都可以运行.但是当我尝试在Heroku上运行"rake db:migrate"时,我收到了一个错误 - 当我尝试在rails控制台上运行相同的代码时,我也遇到了错误(如上所示).
谁能帮我写出合适的WHERE AND条件?或者如果它是对的,有什么区别?
1.
get_primary_status = Trustee.select(:id).where(:secret_key => seckey, :user_id => user_id, :primary_trustee => '1')
Run Code Online (Sandbox Code Playgroud)
2.
get_primary_status = Trustee.select(:id).where(:secret_key => seckey).where(:user_id => user_id).where(:primary_trustee => '1')
Run Code Online (Sandbox Code Playgroud)
3.
get_primary_status = Trustee.select(:id).where('secret_key = (?) AND user_id = (?) AND primary_trustee = (?)', seckey, user, '1')
Run Code Online (Sandbox Code Playgroud) 我的方法exists_else有两个参数:base和fallback.如果base是nil,则返回fallback.如果不是nil,则返回base.一个exists_else(true, false)应该回来的电话true.
如果我使用标准的look- ifstatement,true就像我认为的那样返回:
def exists_else(base, fallback)
unless base.nil?
base
else
fallback
end
end
a = true
exists_else( a, false )
# => true
Run Code Online (Sandbox Code Playgroud)
如果我使用下面显示的内联实现,它将返回false.
def exists_else(base, fallback)
base unless base.nil? else fallback
end
a = true
exists_else( a, false )
# => false
Run Code Online (Sandbox Code Playgroud)
为什么它会false在内联实现中返回?
ruby ×6
activerecord ×2
codeigniter ×1
database ×1
datetime ×1
find ×1
geocoding ×1
gmaps4rails ×1
jquery ×1
regex ×1
syntax ×1
time ×1
validation ×1