Reputation: 11
I am working with a database like this :link to db UML
All my models are link to User with 'belongs_to' (User have 'has_many').
My Contact and Society models are link to the Project one by a 'has_many: through:'.
class Contact < ApplicationRecord
validates_presence_of :name, :firstname, :job, :email, :phone
has_many :projects
has_many :societies, through: :projects
belongs_to :user
end
class Society < ApplicationRecord
has_one_attached :logo
has_many :projects
has_many :contacts, through: :projects
accepts_nested_attributes_for :contacts, allow_destroy: true, reject_if: :all_blank
belongs_to :user
end
class Project < ApplicationRecord
validates_presence_of :name, :value, :description
belongs_to :society
belongs_to :contact
belongs_to :user
end
So when I create a project I can associate a contact and a society to this project. The thing is that I want to be able to associate a contact directly when i create a society. The problem is that I can't access "contact.societies" and "society.contacts" until a project isn't created
My question is, is this possible to add a many to many relation between Contact and Society so that I could be able to associate a society and a contact without created a projet ? Or maybe should I change the association 'has_many: through:' ?
I'm waiting your answers,
Thanks !
Upvotes: 0
Views: 84
Reputation: 27971
Without source code it's hard to know what your issue is, but taking a flying guess, check out the accepts_nested_attributes_for
feature, if that doesn't help you then please edit your question to include some source code, show your model associations, and the code you're trying to run to create your project.
Upvotes: 0