Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. %w(rspec active_record sqlite3 pry byebug).each {|gem| require gem }
  2.  
  3. ActiveRecord::Base.establish_connection(
  4. :adapter => 'sqlite3',
  5. :database => ':memory:' )
  6.  
  7. class CreateTestDB < ActiveRecord::Migration[5.2]
  8. def self.up
  9. create_table :foos do |t|
  10. t.string :name
  11. t.timestamps
  12. end
  13.  
  14. create_table :bars do |t|
  15. t.string :name
  16. t.timestamps
  17. end
  18.  
  19. create_table :comments do |t|
  20. t.references :commentable, polymorphic: true
  21. t.string :blah
  22. t.timestamps
  23. end
  24.  
  25. end
  26. end
  27.  
  28. CreateTestDB.migrate(:up)
  29.  
  30. class Foo < ActiveRecord::Base
  31. has_many :comments, as: :commentable
  32. end
  33.  
  34. class Bar < ActiveRecord::Base
  35. has_many :comments, as: :commentable
  36. end
  37.  
  38. class Comment < ActiveRecord::Base
  39. belongs_to :commentable, polymorphic: true
  40. end
  41.  
  42. RSpec.describe Foo do
  43. it "does not delete the original model when removing through association" do
  44. foo = Foo.create! name: "foo"
  45. foo.comments.create blah: "foo blah blah"
  46.  
  47. bar = Bar.create! name: "bar"
  48. bar.comments.create blah: "bar blah blah"
  49.  
  50. expect(foo.comments.first.blah)
  51. .to eq "foo blah blah"
  52.  
  53. expect(bar.comments.first.blah)
  54. .to eq "bar blah blah"
  55. end
  56. end
Add Comment
Please, Sign In to add comment