Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby数据映射器,表继承_Ruby On Rails_Ruby_Ruby Datamapper - Fatal编程技术网

Ruby on rails Ruby数据映射器,表继承

Ruby on rails Ruby数据映射器,表继承,ruby-on-rails,ruby,ruby-datamapper,Ruby On Rails,Ruby,Ruby Datamapper,我的桌子总是重复这一行 property :created_at, DateTime, :default => DateTime.now, :lazy => [:show] property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show] 我该怎么把它擦干?我是继承还是有一个模块,或者其他什么 是这样的: Class Foo include DataMapper::Resourc

我的桌子总是重复这一行

    property :created_at, DateTime, :default => DateTime.now, :lazy => [:show]
property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show]
我该怎么把它擦干?我是继承还是有一个模块,或者其他什么

是这样的:

Class Foo
include DataMapper::Resource

property :id, Int
property :created_at, DateTime, :default => DateTime.now, :lazy => [:show]
property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show]
end

Class Bar
include DataMapper::Resource

property :id, Int
property :created_at, DateTime, :default => DateTime.now, :lazy => [:show]
property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show]
end
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-constraints'

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
DataMapper::Property::String.length(255)

class BaseTable
  include DataMapper::Resource
  property :id, Serial
  property :created_date, DateTime, :default => DateTime.now
  property :created_by, Integer
  property :updated_date, DateTime, :default => DateTime.now
  property :updated_by, Integer
end

class User < BaseTable
  property :userid, String
  property :email, String
  property :password, String
end

class Account < BaseTable
  property :name, String
  property :type, String
  property :location, String
  property :account_number, String
  property :total_balance, Integer
  property :cleared_balance, Integer
  property :marked_as_cleared_balance, Integer
  has n, :transactions, :constraint => :destroy
end

class Transaction < BaseTable
  property :id, Serial
  property :label, String
  property :cleared, Boolean
  property :date, Date
  property :description, String
  property :amount, Integer
  property :note, String
  property :balance, Integer
  belongs_to :account
end

DataMapper.finalize
DataMapper.auto_upgrade!

这些在每个表格中重复了多次。好了,现在我明白你的意思了。这些是rails属性,它们是自动创建的。我不确定是否有办法防止这种情况发生,但这些方法在很多情况下都非常有用。我建议您保留它们,当您了解更多关于Rails的信息时,您将了解它们的用途

至于视图,您需要创建一个控制器方法,并在
config/routes.rb
中定义到这些方法的路由。我建议您进一步了解MVC rails模式。MVC是Rails构建的核心


是一个学习rails的好网站。试着读几篇文章,你将能够很快理解并构建一个完整的应用程序。

我也在做类似的事情。我是这样使用继承的:

Class Foo
include DataMapper::Resource

property :id, Int
property :created_at, DateTime, :default => DateTime.now, :lazy => [:show]
property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show]
end

Class Bar
include DataMapper::Resource

property :id, Int
property :created_at, DateTime, :default => DateTime.now, :lazy => [:show]
property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show]
end
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-constraints'

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
DataMapper::Property::String.length(255)

class BaseTable
  include DataMapper::Resource
  property :id, Serial
  property :created_date, DateTime, :default => DateTime.now
  property :created_by, Integer
  property :updated_date, DateTime, :default => DateTime.now
  property :updated_by, Integer
end

class User < BaseTable
  property :userid, String
  property :email, String
  property :password, String
end

class Account < BaseTable
  property :name, String
  property :type, String
  property :location, String
  property :account_number, String
  property :total_balance, Integer
  property :cleared_balance, Integer
  property :marked_as_cleared_balance, Integer
  has n, :transactions, :constraint => :destroy
end

class Transaction < BaseTable
  property :id, Serial
  property :label, String
  property :cleared, Boolean
  property :date, Date
  property :description, String
  property :amount, Integer
  property :note, String
  property :balance, Integer
  belongs_to :account
end

DataMapper.finalize
DataMapper.auto_upgrade!
需要“rubygems”
需要“dm核心”
需要“dm迁移”
需要“dm约束”
DataMapper.setup(:默认值,“sqlite3://{Dir.pwd}/development.db”)
DataMapper::Property::String.length(255)
类基表
包含数据映射器::资源
属性:id,序列号
属性:created\u date,DateTime,:default=>DateTime.now
属性:创建人,整数
属性:updated_date,DateTime,:default=>DateTime.now
属性:更新者,整数
结束
类用户:销毁
结束
类事务
I不需要更多信息。此日志何时/何地显示?它在控制台里面吗?模型内部?“请尽量解释得更清楚一点。”GuillhermeBarrosavila更新了这篇文章。我经常重复这些字段。不确定是否有模块、类或任何内容。如果你不介意我问的话,我还有另一个问题:如何创建一个在整个数据库创建之后执行的函数?例如,在创建数据库及其所有表之后,我想添加一些视图。我该怎么做?我回答了。马克:如果你愿意的话,希望我能帮上忙。我对西纳特拉的了解还不够,无法回答你这个问题。尽管我知道它是如何工作的,也知道它应该如何完成,但我认为最好是通过互联网查找教程和书籍