class Post < ActiveRecord::Base
end
post = Post.new
Run Code Online (Sandbox Code Playgroud)
如何判断'post'是否是一个未从数据库中提取的新模型?
我需要将一个yaml文件加载到Hash中,
我该怎么办?
我使用'forever'来运行我的应用程序.我想附加到运行环境来检查我的应用程序.那我该怎么办?
想象一下,我有一个Post Model.
并且数据库中的一条记录将是:
23|title_here|content_here|2011-02-20 08:01:55.583222|2011-02-20 08:01:55.583222
Run Code Online (Sandbox Code Playgroud)
最后两个field(2011-02-20 08:01:55.583222|2011-02-20 08:01:55.583222 )是created_at和updated_at字段.
然后,
post = Post.find_by_id(23)
Run Code Online (Sandbox Code Playgroud)
问题:如何在数据库中获取created_at字符串:" 2011-02-20 08:01:55.583222"?
我们知道,post.created_at将返回一个ActiveSupport::TimeWithZone对象,而不是原始字符串.
请帮忙 :)
class C
end
var = "I am a local var outside"
C.class_eval do
def self.a_class_method
puts var
end
end
Run Code Online (Sandbox Code Playgroud)
我知道,这是不正确的,因为def创建了一个新的范围.我也知道use define_method可以在不创建新范围的情况下创建实例方法,但我的观点是如何定义类方法.
我现在感到困惑,我不知道如何删除/销毁连接表中的记录:
class Task < ActiveRecord::Base
belongs_to :schema
belongs_to :to_do
end
class Todo < ActiveRecord::Base
belongs_to :schema
has_many :tasks
end
class Shcema < AcitveRecord::Base
has_many :todos
has_many :tasks, :through => :todos
end
Run Code Online (Sandbox Code Playgroud)
>> sc = Schema.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
...
>> sc.tasks.delete(Task.first) # I just want to delete/destroy the join item here.
# But that deleted/destroyed the Task.first.
Run Code Online (Sandbox Code Playgroud)
如果我只想破坏关系项,我该怎么办?
class C1
unless method_defined? :hello # Certainly, it's not correct. I am asking to find something to do this work.
def_method(:hello) do
puts 'Hi Everyone'
end
end
end
Run Code Online (Sandbox Code Playgroud)
那么,如何判断方法是否定义?
proc = Proc.new do |name|
puts "Thank you #{name}!"
end
def thank
yield
end
Run Code Online (Sandbox Code Playgroud)
proc.call # output nothing, just fine
proc.call('God') # => Thank you God!
thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank &proc.call('God') # Error!
thank proc.call('God') # Error!
# So, what should I do if I have to pass the 'God' to the proc and use the 'thank' method at the same time ?
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
我在我的项目中使用mercurial,每次我通过ssh将新的更改集推送到服务器时,它会要求我输入密码.
然后如何配置mercurial推出没有问密码?
我在Ubuntu 9.10上工作
简单的例子:
class A
end
class B < A
end
Run Code Online (Sandbox Code Playgroud)
那么,我如何判断B类是否继承自A类?是否有某种方法喜欢is_a?或可能被称为is_child_of??
我找不到一个.