Question

My tables repeat this line always

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

How do I DRY this out? Do I inherit, or have a module, or something else?

It's like this:

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

these are repeated multiple times through each of the table

Was it helpful?

Solution

Well, now I understand what you are saying. Those are rails properties, which are created automatically. I'm not sure if there's a way of preventing this from happening, but those are definitely useful in many situations. I suggest you keep them, you will figure out their use as you learn more about Rails.

As for the views, you need to create a controller method and define a route to those methods inside config/routes.rb. I suggest you learn more about the MVC rails pattern. MVC is the core in which Rails is built upon.

http://guides.rubyonrails.org/ is a great website for learning rails. Try reading just a few articles and you'll be able to understand enough to build a full application in no time.

OTHER TIPS

I'm doing something similar. I used inheritance like this:

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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top