Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-First equivalent in Ruby [closed]

I'm suuuuper lazy and don't like making migrations. I want to make models I'm going to use anyway and have something else figure out the migrations for me in ruby because .Net has spoiled me. Is there a gem that will give me active record migrations from models? Can that even be possible since you never explicitly set types in ruby?

like image 905
Justin Avatar asked Jul 13 '26 10:07

Justin


2 Answers

You can use DataMapper instead of ActiveRecord.

Sample Code from the DataMapper Documentation:

require 'rubygems'
require 'dm-core'
require 'dm-migrations'

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'mysql://localhost/test')

class Person
  include DataMapper::Resource
  property :id,   Serial
  property :name, String, :required => true
end

DataMapper.auto_migrate!
like image 177
ovhaag Avatar answered Jul 14 '26 23:07

ovhaag


This doesn't make any sense. Your models pull data their structure from the schema, they don't contain column/table definitions. There is no way to "push" table structure from your models into migrations, it's impossible. The data just isn't there.

This model, as written, may have persist to a table with a thousand columns or one column, there is no way of knowing:

class User < ActiveRecord::Base

end

Just use the Rails generators to produce your models and migrations at the same time.

like image 24
meagar Avatar answered Jul 14 '26 23:07

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!