例如,有一些模型
class Model_1 < ActiveRecord::Base
has_many :images, :as => :imageable
end
class Model_2 < ActiveRecord::Base
# doesn't have has_many association
end
...
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
如何检查该模型是否具有has_many关联?像这样的东西
class ActiveRecord::Base
def self.has_many_association_exists?(:association)
...
end
end
Run Code Online (Sandbox Code Playgroud)
它可以这样使用
Model_1.has_many_association_exists?(:images) # true
Model_2.has_many_association_exists?(:images) # false
Run Code Online (Sandbox Code Playgroud)
提前致谢
有没有办法通过键从哈希中删除并返回删除的值.即此代码:
var a = attributes['a']
delete attributes['a']
Run Code Online (Sandbox Code Playgroud)
单行.像Ruby一样的Smth delete:
a = attributes.delete(:a)
Run Code Online (Sandbox Code Playgroud)