我试图找出用户A是否是用户B的朋友,但我不想要用户A的整个朋友列表.有没有办法,使用Koala/the Graph API简单地找出用户A是否只是使用用户B的Facebook ID与用户B的朋友?
对于这个问题,我将使用以下三个类:
class SolarSystem < ActiveRecord::Base
has_many :planets
scope :has_earthlike_planet, joins(:planets).merge(Planet.like_earth)
end
class Planet < ActiveRecord::Base
belongs_to :solar_system
belongs_to :planet_type
scope :like_earth, joins(:planet_type).where(:planet_types => {:life => true, :gravity => 9.8})
end
class PlanetType < ActiveRecord::Base
has_many :planets
attr_accessible :gravity, :life
end
Run Code Online (Sandbox Code Playgroud)
范围has_earthlike_planet不起作用.它给了我以下错误:
ActiveRecord :: ConfigurationError:找不到名为"planet_type"的关联; 也许你拼错了吗?
我发现这是因为它等同于以下内容:
joins(:planets, :planet_type)...
Run Code Online (Sandbox Code Playgroud)
和SolarSystem没有planet_type关联.我想在like_earthon Planet,has_earthlike_planeton上使用scope SolarSystem,并希望避免重复代码和条件.有没有办法合并这些范围,就像我试图做但却缺少一块?如果没有,我可以用什么其他技术来实现这些目标?
有没有办法找到用户名的@mention名称(数字xxxx_xxxxxx或全名),反之亦然?
查看msg.message对象,有一个用户对象,其中包含消息来自的人员的id,jid和名称.我想找到那个人的@mention名字,也可能是他们在邮件中提到的任何人的用户名.
假设我在CS中有以下课程
class Foo
foo:
one: 1
two: 2
Run Code Online (Sandbox Code Playgroud)
我想要归还class Bar extends Foo其foo财产{one: 1, two: 2, three: 3}.有没有办法在类定义中可以做到这一点Bar,我只是附加three: 3到foo超类上已经存在的属性Foo?
我很好奇是否有可能像我上面解释的那样做.但是,由于我的用例,它不是阻塞问题,因为我可以使用Coffeescript的super调用来解决它,使其成为一个函数.
我目前正在使用Backbone,我有两个类.一个继承自Backbone.Model,另一个继承自第一个类.在第一个类中,我设置defaults属性,以便在创建此模型时,如果它们未被传入,则设置实例变量.从我的第一个类继承的类有一个额外的键值对,以添加到此defaults对象如果我想覆盖默认值,情况也是如此.
Backbone中对象的默认值是通过使用Underscore的result方法获得的,因此在这种情况下快速解决方法是简单地创建defaults一个返回相同对象的函数.在Coffeescript中,这非常容易,变成:
class Foo
foo: ->
one: 1
two: 2
Run Code Online (Sandbox Code Playgroud)
然后Bar你可以做以下事情:
class Bar extends Foo
foo: ->
_.extends super, three: 3
Run Code Online (Sandbox Code Playgroud) 我正在使用RSpec 2开发Rails 3 Web应用程序,我们正在使用Devise进行身份验证.我们的一个(很快)很多控制器都要求用户登录.我知道Devise提供了sign_in测试助手,但它可以与RSpec或Mocha模拟对象一起使用吗?
我最初尝试过@user = mock_model(User)用户是Devise类的地方.这不是一起工作sign_in :user, @user的get 'index'会重定向到登录表单.
有没有人有使用Devise测试的经验并且可以提供帮助?
我正在尝试进行黄瓜测试,它们似乎随机停止.访问了一个新页面,但页面上没有任何内容呈现,除了Retry later以下屏幕截图.

我在OS X 10.9.3,Chrome 35.0.1916.114上运行bundle exec cucumber.如果我更改了javascript驱动程序,它也会发生在Firefox和Firefox中.
有时候我会喜欢我的Heroku控制台做一些不同的事情,但仅限于那个例子.虽然我能够通过命令行参数控制它,但它可能会有点长/乱.
我正在尝试在启动控制台时将临时ENV变量传递给Heroku,但到目前为止还无法做到.我想跑,SOME_VAR=some_value heroku run console --app myapp但一旦Rails靴子,我没有ENV['SOME_VAR'].
我知道我可以设置它然后从Heroku网站删除它,但重新启动我想避免的服务器.
有没有办法将临时ENV变量传递给Heroku上运行的Rails应用程序,而无需在Heroku仪表板中设置它们?
我正在尝试查找是否存在更多的Rails-y方法来生成以下查询
# The purpose of this query is to select Users who have not updated
# their user record.
SELECT `users`.* FROM `users` WHERE `users`.`created_at` = `users`.`updated_at`
Run Code Online (Sandbox Code Playgroud)
我可以使用Arel完成以下任务:
arel_where = User.arel_table[:created_at].eq(User.arel_table[:updated_at])
User.where(arel_where)
#=> … WHERE `users`.`created_at` = `users`.`updated_at`
Run Code Online (Sandbox Code Playgroud)
但是,如果不访问表并使用其相等方法,则无法使用哈希语法的任何组合来执行此操作。以下两个由于将密钥转换为字符串而失败。
User.where(created_at: :updated_at)
#=> … WHERE `users`.`created_at` = 'updated_at'
User.where(created_at: User.arel_table[:updated_at])
#=> … WHERE `users`.`created_at` = '--- !ruby/struct:Arel::Attributes::Attribu…
Run Code Online (Sandbox Code Playgroud)
虽然不是硬性要求,但我尝试避免使用类似以下的字符串:
User.where('created_at = updated_at')
#=> WHERE (created_at = updated_at)
Run Code Online (Sandbox Code Playgroud)
有时我会分析我的日志,这与使用Arel的第一个查询之间的查询差异需要解决。
是否存在包含可从局部内部访问的局部名称的变量?
render :partial => 'foo'
Run Code Online (Sandbox Code Playgroud)
在_foo.haml中:
.name
= partial_name # would output "foo"
Run Code Online (Sandbox Code Playgroud) 我制作了一个示例JavaScript文件来说明我的困惑.
(function() { console.log(true == true); })()
Run Code Online (Sandbox Code Playgroud)
这应该会导致两个错误; 首先使用==代替===,而第二个是缺少分号.但是,当我跑
var jshint = require('jshint').JSHINT;
jshint('(function() { console.log(true == true); })()');
Run Code Online (Sandbox Code Playgroud)
在节点REPL中,当我不期望时,我会收到错误.我期望没有错误的原因是因为在JSHint文档中,它说:
第二个参数[to jshint]是一个可选的控制JSHINT操作的选项对象.大多数选项都是布尔值:它们都是可选的,默认值为false.
我希望这不会产生错误,因为我没有定义任何选项所以所有都应该是假的.意外行为的最后一部分是设置选项似乎什么都不做.以下两次对jshint的调用也会产生相同的错误:
var myFile = '(function() { console.log(true == true); })()';
jshint(myFile, {eqeqeq: false, asi: false});
jshint('/*jshint eqeqeq:false, asi:false */\n' + myFile);
Run Code Online (Sandbox Code Playgroud)
我不确定JSHint选项是如何工作的,我很可能会错误地解释文档.我很高兴知道我的jshint上述调用或我的假设中的内容是不正确的,或者JSHint是否确实存在问题.
我认为这是一个错误,我将查找将其提交给Google的位置,但我认为我会先在这里提出问题,以防万一我做错了什么或误解了某些内容。我无法让Google的地址解析器始终使用区域偏差。例如,以下方法确实有效:
https://maps.googleapis.com/maps/api/geocode/json?address=boston 返回马萨诸塞州波士顿https://maps.googleapis.com/maps/api/geocode/json?address=boston®ion=uk 返回英格兰林肯郡波士顿但是以下内容不起作用
https://maps.googleapis.com/maps/api/geocode/json?address=manchester 返回英国曼彻斯特https://maps.googleapis.com/maps/api/geocode/json?address=manchester®ion=us 仍返回英国曼彻斯特对于无区域请求,结果都未出现在波士顿,林肯郡和康涅狄格州的曼彻斯特,因此我认为它们应该发挥相同的作用。但是,请务必注意,它可能取决于手动区域输入。以下操作无法正常工作,并且都返回马萨诸塞州波士顿:
https://maps.googleapis.com/maps/api/geocode/json?address=boston®ion=ca 应该是安大略省的波士顿https://maps.googleapis.com/maps/api/geocode/json?address=boston®ion=us-ky 应该是肯塔基州波士顿@ dr-molle接受的答案是正确的。但是,您使用的是Google的JS API,因此无法指定components。您可以在中指定边界区域bounds。要找到边界,您需要LatLng一个矩形框的NE和SW点的对象。对于CT,我做了以下工作:
CT_NE_POINT = new google.maps.LatLng(42.05, -71.783333);
CT_SW_POINT = new google.maps.LatLng(40.966667, -73.733333);
CT_BOUNDS = new google.maps.LatLngBounds(CT_SW_POINT, CT_NE_POINT);
...
geocoder = new google.maps.Geocoder();
geocoder.geocode({address: 'manchester', bounds: CT_BOUNDS}, someCallbackFunction);
Run Code Online (Sandbox Code Playgroud)
componentRestrictionsJavaScript API确实接受一个componentRestrictions对象。您会看到文档未显示componentRestrictions在对象文字中,但确实在下面对其进行了描述(此处未提及它是对象)。上面第一次编辑中的代码可以简化为:
geocoder = new google.maps.Geocoder();
geocoder.geocode({
address: 'manchester',
componentRestrictions: { administrativeArea: 'CT' }
}, someCallbackFunction);
Run Code Online (Sandbox Code Playgroud) arel ×1
coffeescript ×1
cucumber ×1
devise ×1
geocoding ×1
google-maps ×1
heroku ×1
hubot ×1
inheritance ×1
javascript ×1
jshint ×1
koala ×1
node.js ×1
rspec ×1
tdd ×1