Ruby on Rails helps with models, namespace and many-to-many reactions

0

I have problems managing the namespace , I have the following models:

class Administradora::Owner < ApplicationRecord
  has_many :owner_mails, foreign_key: :administradora_owner_id
  has_many :junta_mails, through: :owner_mails
end

class Junta::Mail < ApplicationRecord
  has_many :owner_mails, class_name: "OwnerMail", foreign_key: :junta_mail_id
  has_many :adminstradora_owners, through: :owner_mails
end

class OwnerMail < ApplicationRecord
  belongs_to :administradora_owner, foreign_key: :owner_id, primary_key: :administradora_owner_id
  belongs_to :junta_mail, foreign_key: :mail_id, primary_key: :mail_id
end

By console I can do the following:

Administradora::Owner.first.owner_mails
Junta::Mail.find(4).owner_mails

It works fine, but if I try:

x = OwnerMail.create(administradora_owner_id: 4,junta_mail_id: 3, created_at: "2016/04/04", updated_at: "2016/04/04")
   (0.2ms)  BEGIN
   (0.5ms)  ROLLBACK

The id 4 and 3 exist in their respective tables and the error is:

  

ActiveRecord :: RecordInvalid: Validation failed: Administrator owner   must exist, Board mail must exist

I do not know what may be happening.

    
asked by josedes 27.06.2017 в 18:15
source

3 answers

1

There are several points that can cause the behavior you describe:

  • As highlighted yorodm in your response , it is necessary to use class_name to specify the class when the models are in different namespaces (you can see more information, in English, here ).

  • Verify that the names of the id in your tables are correct; For example, if you are following the rails standards, administradora_owner_id should be simply owner_id (this can be verified in your migrations).

  • You only need to specify foreign_key / primary_key for the columns of id that do not follow the rails standards, otherwise you can omit them; but in any case you must be consistent.

    For example in Junta::Mail you have:

    has_many :owner_mails, class_name: "OwnerMail", foreign_key: :junta_mail_id
    

    While in OwnerMail you have:

    belongs_to :junta_mail, foreign_key: :mail_id, primary_key: :mail_id
    

    Both refer to id of Junta::Mail , but with different column.

  • Considering the above, your models could be modified to look like this 1 :

    class Administradora::Owner < ApplicationRecord
      has_many :owner_mails
      has_many :mails, through: :owner_mails
    end
    
    class Junta::Mail < ApplicationRecord
      has_many :owner_mails
      has_many :owners, through: :owner_mails
    end
    
    class OwnerMail < ApplicationRecord
      belongs_to :owner, class: "Administradora::Owner"
      belongs_to :mail, class: "Junta::Mail"
    end
    

    1 I assume that the columns of id of Administradora::Owner and Junta::Mail are owner_id and mail_id respectively (that is, follow the standards of rails); if not, add the foreign_key of each table.

        
    answered by 27.06.2017 в 20:19
    0

    Probably not exactly the solution you want but whenever I use models in different modules I use the class_name option to create relationships. It would look like this:

    belongs_to ... :class_name => 'Administradora::Owner'
    
        
    answered by 27.06.2017 в 18:57
    0

    In fact, I was able to solve it in the following way:

    class Administradora::Owner < ApplicationRecord
      has_many :owner_mails, foreign_key: :administradora_owner_id, dependent: :destroy
      has_many :junta_mails, through: :owner_mails
      :administradora_owner_id
    end
    
    class Junta::Mail < ApplicationRecord
      has_many :owner_mails, foreign_key: :junta_mail_id, dependent: :destroy
      has_many :administradora_owners, through: :owner_mails
    end
    
    class OwnerMail < ApplicationRecord
      belongs_to :administradora_owner,  class_name: "Administradora::Owner", foreign_key: :administradora_owner_id
      belongs_to :junta_mail,  class_name: "Junta::Mail", foreign_key: :junta_mail_id
    end 
    

    I can now create and I can search the join table through the models of Administradora::Owner and Junta::mail

        
    answered by 27.06.2017 в 22:10