Guest User

Untitled

a guest
Mar 30th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. rom = ROM.container(:sql, 'sqlite::memory') do |conf|
  2. conf.gateways[:default].use_logger(Logger.new($stdout))
  3.  
  4. conf.default.create_table(:users) do
  5. primary_key :id
  6. column :login, String
  7. end
  8.  
  9. conf.default.create_table(:tasks) do
  10. primary_key :id
  11. foreign_key :user_id, :users
  12. column :name, String, null: false
  13. end
  14.  
  15. conf.relation(:users) do
  16. schema(:users, infer: true) do
  17. end
  18. end
  19.  
  20. conf.relation(:tasks) do
  21. schema(:tasks, infer: true) do
  22. end
  23.  
  24. def for_users(users)
  25. # where(employee_id: users.map { |tuple| tuple[:employee_id] })
  26. where(user_id: 1)
  27. end
  28. end
  29. end
  30.  
  31. user = rom.relations[:users]
  32. .changeset(:create, login: 'jane@doe.org')
  33. .commit
  34.  
  35. rom.relations[:tasks].changeset(
  36. :create,
  37. [
  38. { name: 'ble ble', user_id: user[:id] },
  39. { name: 'ble', user_id: user[:id] }
  40. ]
  41. ).commit
  42.  
  43. p rom.relations[:users].combine_with(rom.relations[:tasks].for_users).to_a
  44. # It fetches both relations:
  45. # INFO -- : (0.000088s) SELECT `users`.`id`, `users`.`login` FROM `users` ORDER BY `users`.`id`
  46. # INFO -- : (0.000106s) SELECT `tasks`.`id`, `tasks`.`user_id`, `tasks`.`name` FROM `tasks` WHERE (`user_id` = 1) ORDER BY `tasks`.`id`
  47.  
  48. # buth finally fails probably on mapping:
  49. /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/processor/transproc.rb:40:in `inject_union_value': [[:id], [:user_id], [:name]] attribute: block is required for :from with union value. (ROM::MapperMisconfiguredError)
  50. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
  51. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
  52. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/composite.rb:30:in `call'
  53. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/array.rb:42:in `block in map_array'
  54. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/array.rb:42:in `map'
  55. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/array.rb:42:in `map_array'
  56. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
  57. from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
  58. from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `block in call'
  59. from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `each'
  60. from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `reduce'
  61. from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `call'
  62. from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-core-4.1.0/lib/rom/relation/combined.rb:64:in `call'
  63. from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-core-4.1.0/lib/rom/relation/materializable.rb:13:in `to_a'
Add Comment
Please, Sign In to add comment