小编Kva*_*ass的帖子

Rails - 使用没有STI的类型列?

我想使用一个名为type不调用单表继承(STI)的列 - 我只想type成为一个普通的列来保存一个String.

如果没有Rails期望我有单表继承并抛出异常,我怎么能这样做The single-table inheritance mechanism failed to locate the subclass...This error is raised because the column 'type' is reserved for storing the class in case of inheritance.呢?

关于如何做到这一点的任何想法?

ruby-on-rails single-table-inheritance ruby-on-rails-3

77
推荐指数
4
解决办法
3万
查看次数

Ruby Exceptions - 为什么"else"?

我试图理解Ruby中的异常,但我有点困惑.我正在使用的教程说,如果发生的异常与rescue语句识别的任何异常都不匹配,您可以使用"else"来捕获它:

begin  
# -  
rescue OneTypeOfException  
# -  
rescue AnotherTypeOfException  
# -  
else  
# Other exceptions
ensure
# Always will be executed
end
Run Code Online (Sandbox Code Playgroud)

但是,我在后面的教程"救援"中也看到了没有指定的异常:

begin
    file = open("/unexistant_file")
    if file
         puts "File opened successfully"
    end
rescue
    file = STDIN
end
print file, "==", STDIN, "\n"
Run Code Online (Sandbox Code Playgroud)

如果你能做到这一点,那么我是否需要使用其他?或者我可以像这样在最后使用通用救援?

begin  
# -  
rescue OneTypeOfException  
# -  
rescue AnotherTypeOfException  
# -  
rescue
# Other exceptions
ensure
# Always will be executed
end
Run Code Online (Sandbox Code Playgroud)

ruby exception-handling

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

Rails - 创建和创建!方法,RoR 3教程

所以我知道bang(感叹号)和非bang方法之间的区别通常是方法是修改对象本身还是返回一个单独的修改对象,保持原始不变.

然后在本书的第6章中构建User模型时,我遇到了这个User.create方法,它创建了一个新模型并将其保存到数据库中.在Michael Hartl的Ruby on Rails 3教程中,他写道,该User.create!方法"就像create方法一样工作......除非它在创建失败时引发ActiveRecord :: Record-Invalid异常".

我很困惑.是User.create!方法不是下面的Ruby"砰-公约"还是我完全失去了一些东西?如果他遵循惯例,User.create!如果它是一个类方法,如何修改self?

ruby ruby-on-rails-3 railstutorial.org

37
推荐指数
2
解决办法
3万
查看次数

Heroku - 从多台计算机部署?

我有两台计算机,我希望它们都可以从github上的同一个repo中拉/推,也可以部署到heroku上的同一个应用程序.我知道Heroku允许你在另一台计算机上克隆它的git repo,所以它也可以链接起来,但我不希望第二个克隆heroku repo - 我希望它克隆git repo然后让两台计算机都是当任何一个部署时能够更新heroku ...我该怎么做?这两个已经成功连接到同一个github但我现在想要将它们连接到同一个heroku应用程序.

git heroku ruby-on-rails-3

32
推荐指数
1
解决办法
6616
查看次数

为什么"抽象覆盖"不需要在子程序中单独"覆盖"?

我阅读了Scala中的Programming编程部分abstract override,但是我仍然对这些修饰符的加入所表示的内容感到困惑.使用这些修饰符的代码片段粘贴在下面:

trait Doubling extends IntQueue {
    abstract override def put(x: Int) { super.put(2 * x) }
}
Run Code Online (Sandbox Code Playgroud)

特别是,我对abstract这种情况的目的感到困惑,以及为什么我们不能简单地用override关键字实现预期的结果.如果我们没有包含电话super,我们是否需要关键字abstract?为什么或者为什么不?我正在寻找这个关键字组合的详细解释,因为它与可堆叠特征有关.

scala traits

29
推荐指数
2
解决办法
4186
查看次数

Rails - 自我与@

我正在关注Michael Hartl的RoR教程,它涵盖了密码加密的基础知识.这是目前的用户模型:

class User < ActiveRecord::Base
    attr_accessor :password

    attr_accessible :name, :email,: password, :password_confirmation

    email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/
                                              #tests for valid email addresses.

    validates :name, :presence => true,
                     :length => {:maximum => 50}
    validates :email, :presence => true,
                      :format => {:with => email_regex},
                      :uniqueness => {:case_sensitive => false}
    validates :password, :presence => true,
                         :length => {:maximum => 20, :minimum => 6},
                         :confirmation => true

    before_save :encrypt_password

    private

    def encrypt_password
        self.encrypted_password = encrypt(password)
    end

    def encrypt(string)
        string
    end
end
Run Code Online (Sandbox Code Playgroud)

我发布了一个关于before_save不工作的上一个问题,事实证明我不小心做的是将我的encrypt_password写成:

def …
Run Code Online (Sandbox Code Playgroud)

ruby instance-variables self ruby-on-rails-3

17
推荐指数
1
解决办法
4687
查看次数

需要没有.rb扩展名的ruby文件?

我有一个没有.rb扩展名的ruby文件,而是在文件开头用shebang标识为ruby代码:#!/usr/bin/env ruby.我想require在另一个ruby文件中的这个文件中的代码,但它似乎有一个问题,因为require自动将.rb扩展名附加到它寻找的文件.是否有任何方法可以抑制此行为并使require仅在给出名称时查找该文件?

ruby shebang require

15
推荐指数
1
解决办法
2735
查看次数

Scala - 如何在运行时从外部文件编译代码?

我想设计一个Scala程序,它接受Scala文件作为参数,可以自定义程序的执行.特别是,我想在运行时提供包含将由程序调用的方法的实现的文件.如何正确依赖外部文件并在运行时动态调用其方法?理想情况下,我还希望这些文件能够依赖于我的程序中的方法和类.

示例场景:我有一个包含该行的函数val p: Plant = Greenhouse.getPlant(),并且GreenhousegetPlant方法的类在将在运行时提供的其中一个文件中定义.在该文件中,该方法getPlant返回a Rose,where Rose <: PlantPlant在原始程序中定义.假设文件仅在运行时加入而不是在编译时加入,我如何实现(或近似)这种相互依赖性?

scala external-dependencies scala-compiler

12
推荐指数
2
解决办法
3902
查看次数

为Notepad ++添加关键字到Ruby语法高亮显示

我正在尝试添加requireinclude作为Notepad ++的Ruby关键字,但我遇到了一些麻烦.我修改了langs.model.xml文件的ruby语言标记,如下所示:

<Language name="ruby" ext="rb rbw" commentLine="#">
  <Keywords name="instre1">__FILE__ and def end in or self unless __LINE__
     begin defined? ensure module redo super until BEGIN break do false
     next rescue then when END case else for nil retry true while
     alias class elsif if not return undef yield require include
</Keywords> 
Run Code Online (Sandbox Code Playgroud)

但是,更新这甚至之后requireinclude仍然没有被高亮显示为其他关键字.有人可以建议吗?

ruby notepad++

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

RVM在gemset中看到gems

如何查看给定gemset中所有gem的列表?是否可以一次使用多个gemsets或只使用一个?

rvm ruby-on-rails-3

10
推荐指数
1
解决办法
4357
查看次数