小编Ada*_*sek的帖子

如何在Rails 3.1中复制class_inheritable_accessor的行为?

从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

ruby inheritance ruby-on-rails ruby-on-rails-3.1

14
推荐指数
1
解决办法
2551
查看次数

如何将jQuery.tmpl模板呈现为字符串?

对于文档jquery.tmpl用途.appendTo插入模板到在渲染过程中的DOM:

$.tmpl( myTemplate, myData ).appendTo( "#target" );
Run Code Online (Sandbox Code Playgroud)

我试图从另一个模板引擎转换现有应用程序,我的代码需要先将模板呈现为字符串,然后再将其添加到DOM中.这可能吗?怎么办?

jquery templates jquery-plugins jquery-templates

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

Generic方法可以同时处理Reference和Nullable Value类型吗?

我有一系列的扩展方法来帮助对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).我该如何实现这种行为?

.net c# generics

12
推荐指数
1
解决办法
4017
查看次数

VB.Net中的匿名类初始化

我想在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)

谢谢.

c# vb.net anonymous-types

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

如何将分解为数据库的模型构建为36个表?

我有超过10亿个域名记录,而不是将它们全部放在一个表中,我决定将它们分成36个表(每个表的数据库结构相同).

有一个基于域名第一个字符的表(ex tables:domains_a... domains_z).

如何Domain在rails中创建单个模型,根据指定的字符自动在这些表之间切换?

postgresql ruby-on-rails ruby-on-rails-5

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

如何让Linq to SQL识别动态存储过程的结果集?

我正在使用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)

c# sql sql-server stored-procedures linq-to-sql

8
推荐指数
2
解决办法
6551
查看次数

LINQ to SQL支持哪些版本的SQL Server?

SQL Server 2000可以用作LINQ to SQL的数据库吗?

LINQ to SQL是否依赖于特定版本的Microsoft SQL Server?

linq sql-server linq-to-sql

7
推荐指数
1
解决办法
3941
查看次数

Ruby on Rails NameError:未初始化的常量

我只是设置了一个新的迁移和模型关系,并且在测试表之间的关系时在控制台中我得到以下错误: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)

ruby-on-rails

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

jQuery.data不再适用于窗口?

近日笔者从提升我们项目的jQuery的文件1.4.21.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)

有点凌乱,但将事件连接到普通对象的能力是令人信服的.

javascript jquery javascript-events

7
推荐指数
1
解决办法
3649
查看次数

这个Ruby参数约定来自哪里?

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等)

ruby middleware conventions query-parameters

7
推荐指数
1
解决办法
678
查看次数