是否可以在Active Record模型中使用委托并使用类似的条件:if?
class User < ApplicationRecord
delegate :company, :to => :master, :if => :has_master?
belongs_to :master, :class_name => "User"
def has_master?
master.present?
end
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个动态界面.我的模型类存在的地方,我的控制器是在启动应用程序时动态创建的.
一切都发生在创建资源的路径文件中!
ActionController::Routing::Routes.draw do |map|
map.namespace :admin do |admin|
TestAdmin.models.each do |m|
admin.resources m.to_s.tableize.to_sym
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后是我的BeAdmin类,它执行以下操作:
module TestAdmin
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def beadmin(options = {})
namespace_name = "Admin"
class_name = "#{self.to_s.pluralize.capitalize}Controller"
klass = namespace_name.constantize.const_set(class_name, Class.new(ApplicationController))
klass.module_eval do
def index
render :text => "test"
end
end
end
end
def self.models
all_models = []
Dir.chdir(File.join(Rails.root, "app/models")) do
Dir["**/*.rb"].each do |m|
class_name = m.sub(/\.rb$/,"").camelize
klass = class_name.split("::").inject(Object){ |klass,part| klass.const_get(part) }
all_models << "#{class_name}" if klass < ActiveRecord::Base && …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序和同步工作中集成了iCloud,但有时我收到此错误:
+[PFUbiquityTransactionLog loadPlistAtLocation:withError:](324): CoreData: Ubiquity:
Encountered an error trying to open the log file at the location: <PFUbiquityLocation:
0x1993c0>: /private/var/mobile/Library/Mobile Documents/ ...
Error: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed.
(Cocoa error 256 - The item failed to download.)"
-[PFUbiquityTransactionLog loadComparisonMetadataWithError:](220): CoreData: Ubiquity:
Error encountered while trying to load the comparison metadata for transaction log:
<PFUbiquityTransactionLog: 0x1a3d60>
transactionLogLocation: <PFUbiquityLocation: 0x1993c0>: /private/var/mobile/Library/Mobile Documents/
transactionNumber: (null)
Error: Error Domain=NSCocoaErrorDomain Code=134302 "The operation couldn’t be completed.
(Cocoa error 134302.)" UserInfo=0x1a50e0 {reason=Error during …Run Code Online (Sandbox Code Playgroud) 我正在尝试重现我与ActiveRecord一起使用的SQL查询:
Post.all(:conditions => ["EXTRACT(MONTH from created_at) = ? AND EXTRACT(YEAR from created_at) = ?", month, year])
Run Code Online (Sandbox Code Playgroud)
但是因为我正在将所有内容转换为DataMapper,所以我正在寻找一种方法来做到这一点......
有人可以帮忙吗?