小编use*_*154的帖子

检查模型实例是否属于rails中的named_scope

假设我有一个命名范围:

class Foo < ActiveRecord::Base
    named_scope :bar, :conditions => 'some_field = 1'
end
Run Code Online (Sandbox Code Playgroud)

这非常适合查询,我定义了一堆有用的named_scopes.我想要的是能够做到这一点:

f = Foo.find(:first)
f.some_field = 1
f.is_bar? #=> true
Run Code Online (Sandbox Code Playgroud)

酒吧?' 如果模型实例属于命名范围,则method将简单地返回true或false.不管怎么说没有写'is_bar'吗?即使我已经写了一个很好的方法来检查'is_bar'是否有问题?如果我没记错的话,DRY很好,所以任何帮助都会非常感激/

named-scope boolean ruby-on-rails

8
推荐指数
1
解决办法
3572
查看次数

Ubuntu终端的声音通知

当运行诸如bundle install(对于Ruby应用程序)的命令时,我希望在命令完成时听到一个小的声音(或弹出)通知.我怎样才能做到这一点?

terminal command-line

8
推荐指数
1
解决办法
4722
查看次数

如何在使用Javascript和ERB模板(Rails)时保持DRY

我正在构建一个Rails应用程序,它使用Pusher来使用Web套接字将更新直接推送到客户端.在javascript中:

channel.bind('tweet-create', function(tweet){ //when a tweet is created, execute the following code:
  $('#timeline').append("<div class='tweet'><div class='tweeter'>"+tweet.username+"</div>"+tweet.status+"</div>");
});
Run Code Online (Sandbox Code Playgroud)

这是令人讨厌的代码和演示混合.所以自然的解决方案是使用javascript模板.也许生态或胡子:

//store this somewhere convenient, perhaps in the view folder:
tweet_view = "<div class='tweet'><div class='tweeter'>{{tweet.username}}</div>{{tweet.status}}</div>"

channel.bind('tweet-create', function(tweet){ //when a tweet is created, execute the following code:
    $('#timeline').append(Mustache.to_html(tweet_view, tweet)); //much cleaner
});
Run Code Online (Sandbox Code Playgroud)

这很好,除了我重复自己.胡子模板与我已编写的用于从服务器呈现HTML的ERB模板99%相同.小胡子和ERB模板的预期输出/目的是100%相同:将tweet对象转换为tweet html.

消除这种重复的最佳方法是什么?

更新:即使我回答了我自己的问题,我真的希望看到其他人的其他想法/解决方案 - 因此是赏金!

javascript model-view-controller ruby-on-rails dry mustache

8
推荐指数
1
解决办法
5100
查看次数

ActiveRecord has_many其中表A中的两列是表B中的主键

我有一个模型,Couple,其中有两列,first_person_idsecond_person_id和另一种模式,Person,其主要关键是person_id,有列name

这是我想要的用法:

#including 'Person' model for eager loading, this is crucial for me
c = Couple.find(:all, :include => :persons)[0]
puts "#{c.first_person.name} and #{c.second_person.name}"
Run Code Online (Sandbox Code Playgroud)

那我该怎么做呢?

ruby activerecord ruby-on-rails has-many

7
推荐指数
1
解决办法
4356
查看次数

电信中Erlang的使用

我是一名网络开发人员和一名电信专业的大学生.这意味着我在编程方面很不错,而且我对电信网络有一点了解(在很高的非技术层面).

我一直在读,Erlang被用于整个电信行业(据称其性能).

我想知道无论如何我可以将我的编程技巧与我的电信专业与Erlang结合起来.大部分二郎/电信的东西是封闭源吗?有没有写过Erlang的开源电信项目?

更新:sipwiz的评论让我想到一个比"使用Erlang"更大的问题.如何通过编程利用对电信网络和电信监管环境的高层次理解.我希望这对于SO来说并没有过于偏离主题.

erlang telecommunication

7
推荐指数
1
解决办法
3856
查看次数

使用Parse iOS SDK和RubyMotion

RubyMotion提供了销售第三方代码的这些说明:http://www.rubymotion.com/developer-center/guides/project-management/#_files_dependencies

我正在尝试添加Parse.com的iOS SDK.以下是将其添加到XCode项目的说明:https://parse.com/apps/quickstart#ios/existing.但是,因为我正在使用RubyMotion,所以我没有使用XCode.

我在这里记录了我的尝试:https://github.com/adelevie/RubyMotionSamples/commit/603bf4428995bb203cce7e7e8e6989d6e86bda3b

以下是我得到的错误:https://gist.github.com/2595284

ruby xcode parse-platform rubymotion

7
推荐指数
1
解决办法
4179
查看次数

在Ruby中做什么|| =做什么

我一直在使用Ruby一段时间了,我一直看到这个:

foo ||= bar
Run Code Online (Sandbox Code Playgroud)

它是什么?

ruby

6
推荐指数
2
解决办法
441
查看次数

使用hash或case-statement [Ruby]

一般哪个更好用?:

case n
when 'foo'
 result = 'bar'
when 'peanut butter'
 result = 'jelly'
when 'stack'
 result = 'overflow'
return result
Run Code Online (Sandbox Code Playgroud)

要么

map = {'foo' => 'bar', 'peanut butter' => 'jelly', 'stack' => 'overflow'}
return map[n]
Run Code Online (Sandbox Code Playgroud)

更具体地说,我何时应该使用case语句,何时应该只使用哈希?

ruby hash case

6
推荐指数
2
解决办法
3567
查看次数

与ActiveSupport中的JSON依赖关系相关的Rails 3和MySQL问题

我使用sqlite构建了我的Rails 3应用程序,现在我正在尝试切换到MySQL.

我创建了一个新的MySQL数据库,相应地更改了database.yml,我在我的gemfile(gem 'mysql2', '< 0.3')中添加了一个旧版本的mysql2 gem,它应该可以更好地使用Rails 3.

我可以启动开发服务器.当我访问页面时,我得到一个Mysql2::Error(Table 'twitter_quiz_development.users' doesn't exist).哪个没问题 - 我仍然需要将架构添加到新的数据库中.这是我遇到麻烦的地方:

rake db:schema:load返回这个讨厌的错误.

这里找到的解决方案对我不起作用.gem pristine --all归还这个.

如果有帮助,我正在运行Ubuntu 11.

看来该错误与JSON有关,但我不知道如何解决这个问题.谢谢.

mysql json mysql2 ruby-on-rails-3

6
推荐指数
1
解决办法
424
查看次数

使用CarrierWave上传RESTful文件

我正在尝试为文件上传构建API后端.我希望能够使用具有Base64编码的文件字符串的POST请求上传文件.服务器应解码字符串,并使用CarrierWave保存文件.这是我到目前为止所拥有的:

photo.rb:

class Photo
  include Mongoid::Document
  include Mongoid::Timestamps
  mount_uploader :image_file, ImageUploader
end
Run Code Online (Sandbox Code Playgroud)

image_uploader.rb:

class ImageUploader < CarrierWave::Uploader::Base
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end
Run Code Online (Sandbox Code Playgroud)

Rails控制台:(摘要)

ruby-1.8.7-p334 :001 > img = File.open("../image.png") {|i| i.read}
 => "\377???JFIF\000\001\002\001\000H\000H\000\000\377?Photoshop 3.0\0008BIM\003...
ruby-1.8.7-p334 :003 >   encoded_img = Base64.encode64 img
=> 3af8A\nmLpplt5U8q+a7G2...
ruby-1.8.7-p334 :005 >   p = Photo.new
 => #<Photo _id: 4e21b9a31d41c817b9000001, created_at: nil, updated_at: nil, _type: nil, user_id: nil, image_file_filename: nil> 
ruby-1.8.7-p334 :006 > p.user_id = 1
 => 1 
ruby-1.8.7-p334 :007 > p.image_file = Base64.decode64 encoded_img …
Run Code Online (Sandbox Code Playgroud)

ruby rest file-upload ruby-on-rails carrierwave

6
推荐指数
1
解决办法
8519
查看次数