0

我的桌子总是重复这一行

    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

这些在每个表中重复多次

4

2 回答 2

0

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

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!
于 2015-04-04T00:14:31.833 回答
0

嗯,现在我明白你在说什么了。这些是自动创建的 rails 属性。我不确定是否有办法防止这种情况发生,但这些在许多情况下绝对有用。我建议你保留它们,当你了解更多关于 Rails 的信息时,你会发现它们的用途。

至于视图,您需要创建一个控制器方法并定义到内部这些方法的路由config/routes.rb。我建议您了解有关 MVC 导轨模式的更多信息。MVC 是构建 Rails 的核心。

http://guides.rubyonrails.org/是一个很棒的学习 Rails 的网站。尝试只阅读几篇文章,您将能够理解到足以立即构建完整的应用程序。

于 2013-09-11T06:09:38.080 回答