Models & # 8594; has_many & # 8594; Twice

So, I have a somewhat confusing relationship between Note, Group and User. And I ended up with has_many twice in my model. But I'm currently focused on the Note and Group relationship.

Reference Information. The group may have a note. The user may also have a note. That is why my note is polymorphic. However, I also created a union model called a tag, so that Note can belong to several groups. In my code, however, I ended up with a few "has_many: notes". See all my code below. What would be the right way to do something like this?

Thanks in advance!

note.rb

belongs_to :notable, :polymorphic => true has_many :tags has_many :groups, :through => :tags 

user.rb

 has_many :notes, :as => :notable 

group.rb

 has_many :notes, :as => :notable has_many :tags has_many :notes, :through => :tags 

tag.rb

 belongs_to :note belongs_to :group 
+4
source share
1 answer

You just need to specify a different name.

 class Group has_many :notes, :as => :notable has_many :tags has_many :tagged_notes, :class_name => 'Note', :through => :tags end 

If you only need one note for the part :as => :notable (this was not very clear in your question), you can simply do this:

 class Group has_one :note, :as => :notable has_many :tags has_many :notes, :through => :tags end 

Names must be different. Although with note and notes may not be completely clear what the difference is in other parts of your code.

+5
source

All Articles