小编Tru*_* Lê的帖子

如果多态关联的类型列不指向STI的基本模型,为什么多态关联不适用于STI?

我有一个多态关联和STI的案例.

# app/models/car.rb
class Car < ActiveRecord::Base
  belongs_to :borrowable, :polymorphic => true
end

# app/models/staff.rb
class Staff < ActiveRecord::Base
  has_one :car, :as => :borrowable, :dependent => :destroy
end

# app/models/guard.rb
class Guard < Staff
end
Run Code Online (Sandbox Code Playgroud)

为了使多态关联起作用,根据多态关联的API文档,http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations 我必须设置borrowable_typebase_classSTI模型,在我的情况下是Staff.

问题是:如果borrowable_type设置为STI类,为什么它不起作用?

一些测试来证明它:

# now the test speaks only truth

# test/fixtures/cars.yml
one:
  name: Enzo
  borrowable: staff (Staff)

two:
  name: Mustang
  borrowable: guard (Guard)

# test/fixtures/staffs.yml
staff:
  name: Jullia Gillard

guard:
  name: Joni Bravo …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails polymorphic-associations

41
推荐指数
4
解决办法
2万
查看次数

如何在从注册表中提取图像时修复Docker的"错误拉动图像...太多重定向"失败?

我在OS X 10.10上通过CoreOS和Vagrant运行Docker.

当我docker pull ubuntu在CoreOS中运行时,出现以下错误:

$ docker pull ubuntu
Pulling repository ubuntu
cc0067db4f11: Error pulling image (precise) from ubuntu, endpoint: https://cdn-registry-1.docker.io/v1/, Get https://cdn-registry-1.docker.io/v1/images/cc0067db4f11198ef6fd0435f96a973e559b1cebfceb2bc8e4fe527b32045c2f/ancestry: dial tcp: lookup cdn-registry-1.dcc0067db4f11: Error pulling image (precise) from ubuntu, Get https://cdn-registry-1.docker.io/v1/images/cc0067db4f11198ef6fd0435f96a973e559b1cebfceb2bc8e4fe527b32045c2f/ancestry: dial tcp: lookup cdn-registry-1.docker.io on [10.0.2.3]:53: too many redirects 
6006e6343fad: Error pulling image (quantal) from ubuntu, endpoint: https://cdn-registry-1.docker.io/v1/, Get https://cdn-registry-1.docker.io/v1/images/6006e6343fadaaeb5dd98436a3cd17eac8c03cabca1ed4c8778d039e72ebcc9c/ancestry: dial tcp: lookup cdn-registry-1.d6006e6343fad: Error pulling image (quantal) from ubuntu, Get https://cdn-registry-1.docker.io/v1/images/6006e6343fadaaeb5dd98436a3cd17eac8c03cabca1ed4c8778d039e72ebcc9c/ancestry: dial tcp: lookup cdn-registry-1.docker.io on [10.0.2.3]:53: too many redirects 
7656cbf56a8c: …
Run Code Online (Sandbox Code Playgroud)

macos docker coreos

27
推荐指数
2
解决办法
1万
查看次数

为typeahead.js远程选项的ajax调用添加额外数据

我想问一下如何为remote选项的AJAX调用添加额外的参数.我有以下简单的形式来搜索资格(它在Coffeescript中):

$('#search_qualification').typeahead
  name:  'qualification'
  limit: 50
  remote:
    url: "/search/qualification?term=%QUERY"
Run Code Online (Sandbox Code Playgroud)

我想通过AJAX调用发送另一个参数,也就是说state,通常使用jQuery AJAX调用,我会这样做:

$.ajax
  url: "/search/qualification"
  dataType: "json"
  data:
    term: request.term
    state: $("#state").val()
Run Code Online (Sandbox Code Playgroud)

我完全无法适应这种类型的typeahead.js,有人可以帮帮我吗?非常感谢

typeahead.js

5
推荐指数
1
解决办法
4573
查看次数

为什么非显式splat参数加上默认参数是Ruby 1.9中方法定义的错误语法?

我想问一下为什么在Ruby-1.9.3-p0中有一个splat param1和一个带有默认值赋值的param2,如下所示:

def my_method(*param1, param2 = "default"); end

回报

SyntaxError: (irb):1: syntax error, unexpected '=', expecting ')'

我的解决方法明确地将param1包装在括号中,如下所示:

def my_method((*param1), param2 = "default"); end

非常感谢

ruby

4
推荐指数
1
解决办法
463
查看次数

如何用Ruby确定PDF页面的维度?

是否有一个好的库可以确定PDF页面的维度?

我知道一种方法是使用rghostgem将pdf转换为png然后使用image_sizegem来读取png维度.我不喜欢这种方法.

ruby pdf

4
推荐指数
1
解决办法
1424
查看次数

number_to_currency舍入精度错误

我很好奇为什么Ruby渲染小数,精度为2不一致.

例如:

helper.number_to_currency 9.995
Run Code Online (Sandbox Code Playgroud)

=>"9.99美元"

同时

helper.number_to_currency 10.995
Run Code Online (Sandbox Code Playgroud)

=>"11.00"......应该是"10.99美元"吗?

ruby-on-rails helper

2
推荐指数
1
解决办法
1765
查看次数