我目前正在使用find或create方法来更新我用来存储缓存信息的关联记录,但我想知道是否有一些类似于build的更简单的替代方法,因为该对象是一种has_one关系.仅使用build_的问题在大多数情况下对象将存在并且需要更新.我可以使用一堆ifs检查,但想知道是否有比我目前使用的更好的rails-fu.
def self.update_caches(data)
cache = SpaceCache.find_or_create_by_space_id(@space.id)
cache.rating = ((data.ratings.sum / data.ratings.size)/20).round
cache.price_min = @space.availables.recent.average(:minimum)
cache.price_avg = @space.availables.recent.average(:price)
cache.save
end
Run Code Online (Sandbox Code Playgroud)
奖金:
我也在这里读到:
http://m.onkey.org/active-record-query-interface
轨道计算方法的平均值,总和等将在3.1中折旧,所以我不确定是否应该更换它们?
count(column, options)
average(column, options)
minimum(column, options)
maximum(column, options)
sum(column, options)
calculate(operation, column, options)
Run Code Online (Sandbox Code Playgroud) scope ruby-on-rails associations model-associations ruby-on-rails-3
我想从复选框中做下面的事情,
每行都有一个复选框,我想在单击复选框时禁用类.room的行中的所有输入字段.
function toggleStatus(link) {
$(link).closest(".room").children(':input').attr('disabled', true);
}
Run Code Online (Sandbox Code Playgroud)
也试过了
function toggleStatus(link) {
$(link).closest(".room").children('input[type=text]').attr('disabled', true);
}
Run Code Online (Sandbox Code Playgroud) 我有2个模型,一个接受另一个的属性,我试图找到一个聪明的方法来使用Factory girl为两者设置数据.
Class Booking
has_many :booking_items
accepts_nested_attributes_for :booking_items
Class BookingItem
belong_to :booking
Run Code Online (Sandbox Code Playgroud)
Factory.define :booking do |f|
f.bookdate Date.today+15.days
f.association :space
f.nights 2
f.currency "EUR"
f.booking_item_attributes Factory.build(:booking_item) # doesn't work
end
Factory.define :booking_item do |f|
f.association :room
f.bookdate Date.today
f.people 2
f.total_price 20
f.association :booking
end
Run Code Online (Sandbox Code Playgroud)
require "spec_helper"
describe Booking do
before(:each) do
@booking = Factory.create(:booking)
end
it "should be valid" do
#needs children to be valid
@booking.should be_valid
end
end
Run Code Online (Sandbox Code Playgroud)
我环顾了rdocs,但似乎无法找到我想要的东西.
我有一个流氓gem(omniauth),它提供了一个包含ASCII-BIT8字符串的数据哈希,我希望将其转换为UTF.
如何将哈希的所有字符串元素强制为UTF,作为某种rails初始化方法?.to_utf8
session[:omniauth] = omniauth.to_utf8
class Hash
def to_utf8
#not really sure what to do here?
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个名为named_scope的Rails,它使用一个条件从表中拉出一周中的特定日期,如下所示:
:conditions => [ 'EXTRACT(DOW FROM bookdate) IN (?)', (1..6).to_a ]
Run Code Online (Sandbox Code Playgroud)
1.6日期范围将是一个变量,具体取决于用户想要的日期,
哪个产生这个SQL
(EXTRACT(DOW FROM bookdate) IN (1,2,3,4,5,6)
Run Code Online (Sandbox Code Playgroud)
我的问题是,一周的日子不是一个简单的范围...即1..6工作正常(周一...周六),但说6..2将无法正常工作(周六 - 周二)...无论是作为红宝石范围还是需要6,7,1,2而不是6,5,4,3,2(假设6..2在红宝石中工作,它没有).
如何创建一个星期几的自定义范围,以适应这样的日期范围?
有任何想法吗?
谢谢,
我有一个像这样的单一嵌套资源:
map.resources :bookings, :member => { :rate => :post } do |booking|
booking.resource :review
end
Run Code Online (Sandbox Code Playgroud)
给我这些路线:
new_booking_review GET /bookings/:booking_id/review/new(.:format) {:controller=>"reviews", :action=>"new"}
edit_booking_review GET /bookings/:booking_id/review/edit(.:format) {:controller=>"reviews", :action=>"edit"}
booking_review GET /bookings/:booking_id/review(.:format) {:controller=>"reviews", :action=>"show"}
PUT /bookings/:booking_id/review(.:format) {:controller=>"reviews", :action=>"update"}
DELETE /bookings/:booking_id/review(.:format) {:controller=>"reviews", :action=>"destroy"}
POST /bookings/:booking_id/review(.:format) {:controller=>"reviews", :action=>"create"}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
<% form_for [@booking, @review] do |f| %>
Run Code Online (Sandbox Code Playgroud)
哪个返回了这个错误:
undefined method `booking_reviews_path' for #<ActionView::Base:0x10572b888>
Run Code Online (Sandbox Code Playgroud)
用我的评论控制器
def new
@review = Review.new
@booking = Booking.find(params[:booking_id])
end
Run Code Online (Sandbox Code Playgroud)
但这有效...如果我明确列出了URL ...
<% form_for :review, :url => booking_review_path(@booking) do |f| %>
Run Code Online (Sandbox Code Playgroud)
这不是什么大问题......但我想知道我做错了什么.
谢谢
我有一系列的复选框,一切正常,除了他们的默认行为已被无意改变,所以我不能再检查它们是奇怪的,因为我使用这个jquery的原因是当他们检查时突出他们周围的李第一名.
有任何想法吗?
//Tag cloud
$(".tag-cloud li").toggle(
function () {
//$(this).filter(":checkbox").attr('checked', 'true');
$(this).addClass("selected");
return;
//return $(this).filter(":checkbox").attr("checked", true);
},
function () {
//$(this).filter(":checkbox").attr('checked', 'false');
$(this).removeClass("selected");
return;
//$("#sortable").submit();
}
);
Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法可以在自定义操作上使用rails验证.
例如,我想做这样的事情:
validates_presence_of :description, :on => :publish, :message => "can't be blank"
Run Code Online (Sandbox Code Playgroud)
我做了基本的验证创建和保存,但是有许多我不想预先要求的东西.即,他们应该能够在不验证所有字段的情况下保存准系统记录,但是我在我的控制器和模型中有一个自定义的"发布"操作和状态,在使用时应验证以确保记录为100%
上面的例子没有用,有什么想法吗?
更新:
我的状态机看起来像这样:
include ActiveRecord::Transitions
state_machine do
state :draft
state :active
state :offline
event :publish do
transitions :to => :active, :from => :draft, :on_transition => :do_submit_to_user, :guard => :validates_a_lot?
end
end
Run Code Online (Sandbox Code Playgroud)
我发现我可以添加防护,但我仍然希望能够使用rails验证而不是在自定义方法上完成所有操作.
我有一张桌子,里面有我想要的4件事......名称,价格,数量,以及具体的日期
每个日期有很多条目:
Name Price Date
Twin Private $25 06/02/09
Double $35 06/02/09
Single $20 06/02/09
Twin Private $25 06/03/09
Double $35 06/03/09
Single $20 06/03/09
Twin Private $25 06/04/09
Double $35 06/04/09
Single $20 06/04/09
Run Code Online (Sandbox Code Playgroud)
我怎样才能将它压缩成:
Name Price_06/02/09 Price_06/03/09 Price_06/04/09
Twin Private $25 $25 $30
Double $35 $35 $50
Single $20 $20 $40
Run Code Online (Sandbox Code Playgroud) I have a simple hash like so { "1234" => "5", "2345" => "6" }
Run Code Online (Sandbox Code Playgroud)
如何创建一个包含键和值的新哈希?像这样:
{ key_id = "1234", value_id = "5" }, { key_id = "2345", value_id = "6" }
Run Code Online (Sandbox Code Playgroud) ruby ×4
arrays ×2
hash ×2
jquery ×2
aggregate ×1
associations ×1
checkbox ×1
children ×1
days ×1
factory-bot ×1
group-by ×1
input ×1
javascript ×1
list ×1
mysql ×1
nested ×1
omniauth ×1
range ×1
routes ×1
routing ×1
rspec ×1
rspec-rails ×1
scope ×1
sql ×1
sql-order-by ×1
validation ×1