如何初始化ActiveRecord Tableless模型? [英] How can I inizialize that ActiveRecord Tableless Model?

查看:113
本文介绍了如何初始化ActiveRecord Tableless模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的模型中,我有:

我使用Ruby on Rails 3和我想初始化一个ActiveRecord Tableless模型。

  class Account< ActiveRecord :: Base 

#以下ActiveRecord Tableless Model语句来自http://codetunes.com/2008/07/20/tableless-models-in-rails/
def self。 columns()
@columns || = [];
end

def self.column(name,sql_type = nil,default = nil,null = true)
columns< ActiveRecord :: ConnectionAdapters :: Column.new(name.to_s,default,sql_type.to_s,null)
end

attr_reader:id,
:firstname,
:lastname,

def initialize(attributes = {})
@id = attributes [:id]
@firstname = attributes [:firstname]
@lastname = attributes [:lastname]
end
end

示例在application_controller.rb文件中,我执行:

  @new_account = Account.new({:id =>1 ,:firstname =>Test name,:lastname =>Test lastname})

@new_account 变量的debug\inspect输出为

 #< Account> 

为什么?我应该如何正确地初始化ActiveRecord Tableless模型并使其工作?

解决方案

将如下所示:

  class Account< ActiveRecord :: Base 

class_inheritable_accessor:columns

def self.columns()
@columns || = [];
end

def self.column(name,sql_type = nil,default = nil,null = true)
columns< ActiveRecord :: ConnectionAdapters :: Column.new(name.to_s,default,sql_type.to_s,null)
end

column:id,:integer
column:firstname,: string
column:lastname,:string

end

然后:

  @new_account = Account.new({:id =>1,:firstname => name,:lastname =>Test lastname})


I am using Ruby on Rails 3 and I would like to inizialize an ActiveRecord Tableless Model.

In my model I have:

class Account < ActiveRecord::Base

  # The following ActiveRecord Tableless Model statement is from http://codetunes.com/2008/07/20/tableless-models-in-rails/
  def self.columns() 
    @columns ||= [];
  end

  def self.column(name, sql_type = nil, default = nil, null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  end

  attr_reader :id,
              :firstname,
              :lastname,

  def initialize(attributes = {})
    @id = attributes[:id]
    @firstname = attributes[:firstname]
    @lastname = attributes[:lastname]
  end
end

If in a controller, for example in the application_controller.rb file, I do:

@new_account = Account.new({:id => "1", :firstname => "Test name", :lastname => "Test lastname"})

a debug\inspect output of the @new_account variable is

"#<Account >"

Why? How I should inizialize properly that ActiveRecord Tableless Model and make it to work?

解决方案

According to that blog post it would have to look like this:

class Account < ActiveRecord::Base

  class_inheritable_accessor :columns

  def self.columns() 
    @columns ||= [];
  end

  def self.column(name, sql_type = nil, default = nil, null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  end

  column :id, :integer
  column :firstname, :string
  column :lastname, :string

end

And then:

@new_account = Account.new({:id => "1", :firstname => "Test name", :lastname => "Test lastname"})

Did you already try it like that?

这篇关于如何初始化ActiveRecord Tableless模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆