Create tableless activerecord objects that support associations and validations.
-
add
gem 'acts_as_tableless'
to your gemfile -
run
bundle install
-
add
acts_as_tableless
to a activerecord model
class User < ActiveRecord::Base has_many :shapes has_many :user_colors has_many :colors, :through => :user_colors has_one :size belongs_to :number attr_accessible :name, :number_id, :letter_id end class Color < ActiveRecord::Base acts_as_tableless column :id, :integer column :name, :string attr_accessible :id, :name belongs_to :user_colors end class Letter < ActiveRecord::Base acts_as_tableless column :id, :integer column :name, :string column :user_id, :integer attr_accessible :id, :name, :user_id has_and_belongs_to_many :users end class Number < ActiveRecord::Base acts_as_tableless column :id, :integer column :name, :string attr_accessible :id, :name has_many :users end class Shape < ActiveRecord::Base acts_as_tableless column :id, :integer column :name, :string column :user_id, :integer attr_accessible :id, :name, :user_id belongs_to :user end class Size < ActiveRecord::Base acts_as_tableless column :id, :integer column :name, :string column :user_id, :integer attr_accessible :id, :name, :user_id belongs_to :user end class UserColor < ActiveRecord::Base belongs_to :user belongs_to :color attr_accessible :user_id, :color_id end
@user = User.create(:name => "User") @user.shapes.create(:name => "Octagon") #=> #<Shape id: 1, name: "Octagon", user_id: 1> @user.colors << Color.create([{:name => "Green"},{:name => "Blue"}]) #=> [#<Color id: 1, name: "Green">, #<Color id: 2, name: "Blue">] @user.size.create(:name => "Large") #=> #<Size id: 1, name: "Large", user_id: 1> @user.number = Number.create(name: "One") #=> #<Number id: 1, name: "One">
More stuff in the test file.
This gem relies on String#singularize which is known to have some problems. To correct errors such as:
uninitialized constant Statu (NameError)
please add something similar to:
ActiveSupport::Inflector.inflections do |inflect| inflect.singular("statuses", "status") inflect.singular("status", "status") end
to your environment.rb file before the Application.initialize!
line
First setup the test database.
cd test/dummy rake db:setup && db:test:prepare
Then from the root dir run the tests with Rake.
cd ../.. # from test/dummy rake test