NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

Issue with the command to sync the indexes #255

Open arabakevin opened 4 years ago

arabakevin commented 4 years ago

Hi, i have two models my first one is "Organiasation"

# frozen_string_literal: true

# @author kzvdev
# Organisation model
class Organisation
  include NoBrainer::Document
  include NoBrainer::Document::Timestamps
  include HasSlug

  # ~~~~ Virtual Fields ~~~~
  attr_accessor :remove_logo

  # ~~~~ Fieds ~~~~~
  field :name, type: String, uniq: true, required: true
  field :logo, type: String
  field :postal_code, type: String
  field :city, type: String
  field :street, type: String
  field :email, type: String
  field :billing_email, type: String
  field :slug, type: String, uniq: true, required: true

  # ~~~~ Special Behaviors ~~~~
  mount_uploader :logo, LogoUploader

  # ~~~~ Associations ~~~~
  belongs_to :created_by_user, class_name: 'User'
  belongs_to :country, validates: false
  has_many :clients
  has_many :ideas
  has_many :join_requests
  has_many :notifications
  has_many :projects
  has_many :tags
  has_many :users

  # Simple index
  index :name

  def to_param
    slug
  end
end

And a second who is named "Project"

# frozen_string_literal: true

# @author kzvdev
# Project model
class Project
  include NoBrainer::Document
  include NoBrainer::Document::Timestamps
  include AASM
  include HasSlug

  # ~~~~ Virtual Fields ~~~~
  attr_accessor :remove_picture

  # ~~~~ Fieds ~~~~~
  field :logo, type: String
  field :name, type: String, required: true, uniq: { scope: :organisation_id }
  field :organisation_id, type: String
  field :created_by_user_id, type: String
  field :slug, type: String, uniq: true, required: true
  field :state, type: String

  # ~~~~ Associations ~~~~
  belongs_to :created_by_user, class_name: 'User'
  belongs_to :organisation
  has_many :ideas

  # Simple index
  index :name

  # ~~~~ Special Behaviors ~~~~
  mount_uploader :logo, LogoUploader

  aasm column: 'state' do
    state :active, initial: true
    state :archived

    event :mark_as_archive do
      transitions from: :active, to: :archived
    end
    event :mark_as_unarchive do
      transitions from: :archived, to: :active
    end
  end

  # @nodoc ~~~~ Scopes ~~~~
  scope :active, -> { where(state: "active") }

  def to_param
    slug
  end
end

So, in the both models i have one index. When i started with an empty database. And i use this command to sync the indexes:

rake nobrainer:sync_indexes

i can see only one index created and this one is only the index of Organisation and the index of the second model is not created.

Thanks. Regards. kevin

nviennot commented 4 years ago

Hey Kevin,

I tried your code (well, without the mount, and aasm and the like). See here: https://gist.github.com/nviennot/9f636ff7ce952ec9e10001e0aabd7a4a

When I run ruby test255.rb, I see in the console the two indexes being created:

D, [2019-11-05T21:59:18.682787 #50235] DEBUG -- : [  24.1ms] r.table("organisations").index_create(:name) {|var_1| var_1[:name]}
D, [2019-11-05T21:59:18.738029 #50235] DEBUG -- : [  36.9ms] r.table("projects").index_create(:name) {|var_1| var_1[:name]}

Edit: I'm guessing the model where the index does not appear might not be loaded. Verify tha the model is loaded (you can debug this by putting a puts "hello" in the model file in equestion.

arabakevin commented 4 years ago

ok thanks. I will test again as soon as possible with your example.

arabakevin commented 4 years ago

Hi, So, i have retested a new time and on my side i have only one index created. I use Docker do you think it's here the issue ?

The puts "hello" working properly.

Which information can i provide you to help me again ?

Thanks for your help. kevin

nviennot commented 4 years ago

Create the smallest test case that is able to reproduce the bug, and then I'll be able to help

arabakevin commented 4 years ago

Hi, as discussed I have created a test repository to help you to reproduce more easily my issue. So, you can find how to reproduce the issue here --> https://github.com/arabakevin/nobrainer-issue-255

Thanks. Cheers. Kevin

zedtux commented 4 years ago

Hey @nviennot,

I'm also looking for this issue too. Could you please give your input?

Thank you.

extem commented 4 years ago

In Rails6, the model may not be loaded during development because the autoloader specification has changed. I solved the problem by setting config.autoloader = :classic

zedtux commented 4 years ago

Thank you @extem, I'll try that.

arabakevin commented 4 years ago

Hi @extem , Thanks for your help i have tested and it's working properly on my side.

Capture d’écran 2019-11-19 à 20 58 39

We can close this issue for me.

kevin.

zedtux commented 4 years ago

@nviennot should the documentation be updated with this option? And the nobrainer post install script to add this option?

nviennot commented 4 years ago

It's probably best to invoke the eager loading of the models (e.g., Zeitwerk::Loader.eager_load_all) when the models should be loaded. In particular, https://github.com/nviennot/nobrainer/blob/master/lib/no_brainer/document/core.rb#L28 should work

zedtux commented 4 years ago

Yes, but I'm more thinking of nobrainer working out-of-the-box with Rails 6.

nviennot commented 4 years ago

Me too, NoBrainer should invoke the proper rails incantation to make sure all models are loaded when needed