Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %w(rspec active_record sqlite3 pry byebug).each {|gem| require gem }
- ActiveRecord::Base.establish_connection(
- :adapter => 'sqlite3',
- :database => ':memory:' )
- class CreateTestDB < ActiveRecord::Migration[5.2]
- def self.up
- create_table :foos do |t|
- t.string :name
- t.timestamps
- end
- create_table :bars do |t|
- t.string :name
- t.timestamps
- end
- create_table :comments do |t|
- t.references :commentable, polymorphic: true
- t.string :blah
- t.timestamps
- end
- end
- end
- CreateTestDB.migrate(:up)
- class Foo < ActiveRecord::Base
- has_many :comments, as: :commentable
- end
- class Bar < ActiveRecord::Base
- has_many :comments, as: :commentable
- end
- class Comment < ActiveRecord::Base
- belongs_to :commentable, polymorphic: true
- end
- RSpec.describe Foo do
- it "does not delete the original model when removing through association" do
- foo = Foo.create! name: "foo"
- foo.comments.create blah: "foo blah blah"
- bar = Bar.create! name: "bar"
- bar.comments.create blah: "bar blah blah"
- expect(foo.comments.first.blah)
- .to eq "foo blah blah"
- expect(bar.comments.first.blah)
- .to eq "bar blah blah"
- end
- end
Add Comment
Please, Sign In to add comment