从Rails 3.1开始,class_inheritable_accessor产生弃用警告,告诉我改为使用class_attribute.但是class_attribute我将以一种重要的方式表现出不同的表现.
典型的用法class_inheritable_attribute是演示者类,如下所示:
module Presenter
class Base
class_inheritable_accessor :presented
self.presented = {}
def self.presents(*types)
types_and_classes = types.extract_options!
types.each {|t| types_and_classes[t] = t.to_s.tableize.classify.constantize }
attr_accessor *types_and_classes.keys
types_and_classes.keys.each do |t|
presented[t] = types_and_classes[t]
end
end
end
end
class PresenterTest < Presenter::Base
presents :user, :person
end
Presenter::Base.presented => {}
PresenterTest.presented => {:user => User, :person => Person}
Run Code Online (Sandbox Code Playgroud)
但使用class_attribute子类会污染他们的父母:
Presenter::Base => {:user => User, :person => Person}
Run Code Online (Sandbox Code Playgroud)
这根本不是理想的行为.是否有其他类型的访问器行为正确,或者我是否需要完全切换到另一种模式?我应该如何复制相同的行为class_inheritable_accessor?
对于文档jquery.tmpl用途.appendTo插入模板到在渲染过程中的DOM:
$.tmpl( myTemplate, myData ).appendTo( "#target" );
Run Code Online (Sandbox Code Playgroud)
我试图从另一个模板引擎转换现有应用程序,我的代码需要先将模板呈现为字符串,然后再将其添加到DOM中.这可能吗?怎么办?
我有一系列的扩展方法来帮助对IDataRecord对象进行空值检查,我目前正在实现这样:
public static int? GetNullableInt32(this IDataRecord dr, int ordinal)
{
int? nullInt = null;
return dr.IsDBNull(ordinal) ? nullInt : dr.GetInt32(ordinal);
}
public static int? GetNullableInt32(this IDataRecord dr, string fieldname)
{
int ordinal = dr.GetOrdinal(fieldname);
return dr.GetNullableInt32(ordinal);
}
Run Code Online (Sandbox Code Playgroud)
等等,对于我需要处理的每种类型.
我想重新实现这些作为通用方法,部分是为了减少冗余,部分是为了学习如何编写通用方法.
我写过:
public static Nullable<T> GetNullable<T>(this IDataRecord dr, int ordinal)
{
Nullable<T> nullValue = null;
return dr.IsDBNull(ordinal) ? nullValue : (Nullable<T>) dr.GetValue(ordinal);
}
Run Code Online (Sandbox Code Playgroud)
只要T是值类型,它就可以工作,但如果T是引用类型,它就不会.
如果T是值类型,则此方法需要返回Nullable类型,否则返回default(T).我该如何实现这种行为?
我想在vb.net中创建一个完全像这样的匿名类:
var data = new {
total = totalPages,
page = page,
records = totalRecords,
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
}
};
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有超过10亿个域名记录,而不是将它们全部放在一个表中,我决定将它们分成36个表(每个表的数据库结构相同).
有一个基于域名第一个字符的表(ex tables:domains_a... domains_z).
如何Domain在rails中创建单个模型,根据指定的字符自动在这些表之间切换?
我正在使用Linq-to-SQL和SQL Server后端(当然)作为项目的ORM.我需要从一个从动态创建的表返回的存储过程中获取结果集.这是proc的样子:
CREATE procedure [RetailAdmin].[TitleSearch] (
@isbn varchar(50), @author varchar(50),
@title varchar(50))
as
declare @L_isbn varchar(50)
declare @l_author varchar(50)
declare @l_title varchar(50)
declare @sql nvarchar(4000)
set @L_isbn = rtrim(ltrim(@isbn))
set @l_author = rtrim(ltrim(@author))
set @l_title = rtrim(ltrim(@title))
CREATE TABLE #mytemp(
[storeid] int not NULL,
[Author] [varchar](100) NULL,
[Title] [varchar](400) NULL,
[ISBN] [varchar](50) NULL,
[Imprint] [varchar](255) NULL,
[Edition] [varchar](255) NULL,
[Copyright] [varchar](100) NULL,
[stockonhand] [int] NULL
)
set @sql = 'select a.storeid, Author,Title, thirteendigitisbn ISBN,
Imprint,Edition,Copyright ,b.stockonhand from ods.items a join …Run Code Online (Sandbox Code Playgroud) SQL Server 2000可以用作LINQ to SQL的数据库吗?
LINQ to SQL是否依赖于特定版本的Microsoft SQL Server?
我只是设置了一个新的迁移和模型关系,并且在测试表之间的关系时在控制台中我得到以下错误:NameError:uninitialized constant.
有谁知道什么是错的?
谢谢
编辑:
这是错误
NameError: uninitialized constant Profile::ProfileNotification
from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in `const_missing'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2199:in `compute_type'
from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2195:in `compute_type'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:156:in `send'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:156:in `klass'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:187:in `quoted_table_name'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_many_association.rb:97:in `construct_sql'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:21:in `initialize'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `new'
from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `profile_notifications'
from (irb):3
Run Code Online (Sandbox Code Playgroud)
ProfileNotification迁移中的代码:
class CreateProfileNotifications < ActiveRecord::Migration
def self.up
create_table :profile_notifications do |t|
t.integer :profile_id, :null => false
t.integer :notification_id, :null => false
t.string :notification_text
t.boolean :checked, :default => false
t.boolean :update_reply, :default => false
t.boolean :opinion_reply, …Run Code Online (Sandbox Code Playgroud) 近日笔者从提升我们项目的jQuery的文件1.4.2来1.4.4,看来,由于1.4.3在路上,我们一直在使用jQuery.data已停止工作.
我们有这个代码:
var events = $(window).data('events');
if (events.scroll)
if (!events.scroll.include(handler))
$(window).scroll(handler);
Run Code Online (Sandbox Code Playgroud)
目的是防止此特定处理程序被多次绑定.
在1.4.2,这工作正常.在1.4.4,events未定义.
function handler() {
//do something
}
$(document).ready(function(){
$(window).scroll(handler);
$('div#test').scroll(handler);
$(window).data('events') -> undefined
$('div#test').data('events') -> Object
});
Run Code Online (Sandbox Code Playgroud)
这个API改变了什么?我该如何列出活动window?
我已将第一行更改为:
var events = $(window).data('__events__').events;
Run Code Online (Sandbox Code Playgroud)
有点凌乱,但将事件连接到普通对象的能力是令人信服的.
Rails和其他框架使用了一块Ruby中间件来解析您发送到服务器的参数到嵌套Hash对象中.
如果将这些参数发送到服务器:
person[id] = 1
person[name] = Joe Blow
person[email] = joe.blow@test.com
person[address][street_address] = 123 Somewhere St.
person[address][city] = Chicago
person[address][zip] = 12345
person[other_field][] = 1
person[other_field][] = 2
person[other_field][] = 3
Run Code Online (Sandbox Code Playgroud)
他们被解析为:
{
:person => {
:id => "1",
:name => "Joe Blow",
:email => "joe.blow@test.com",
:address => {
:street_address => "123 Somewhere St.",
:city => "Chicago",
:state => "IL",
:zip => "12345"
},
:other_field => [ 1, 2, 3 ]
}
}
Run Code Online (Sandbox Code Playgroud)
我相信PHP也支持这一点.任何人都可以告诉我这个约定叫什么,它来自哪里,以及其他语言支持它?(Perl,Python等)
c# ×3
jquery ×2
linq-to-sql ×2
ruby ×2
sql-server ×2
.net ×1
conventions ×1
generics ×1
inheritance ×1
javascript ×1
linq ×1
middleware ×1
postgresql ×1
sql ×1
templates ×1
vb.net ×1