Nikodem Gorak

Joined

360 Experience
3 Lessons Completed
0 Questions Solved

Activity

Posted in How to display multi-day events in SimpleCalendar?

Hi Guys,
I wonder if anyone know how to use simple_calendar - that can be accessible everywhere in application when you click button it opens modal with calendar - how to implement logic model/controller should be in js? Or anyone has some better idea?

Hi!
I have few models:
Article, Tag, ArticlesTag

class Article < ApplicationRecord  
  has_many :articles_tags
  has_many :tags, through: :articles_tags
end
​
class Tag < ApplicationRecord
  has_many :articles_tags
  has_many :articles, through: :articles_tags
end
​
class ArticlesTag < ApplicationRecord
  belongs_to :article
  belongs_to :tag
end

In Articles index view I need to get all articles tags but they should be unique. What is the best way to get them?
Tag - model at the moment is associated only with Article but in the feature it will be with other model as well.
So I can get ArticlesTag.all - this give me all tags in articles and group them by tag name to be unique.
Like this:

 ArticlesTag.all.group_by(&:tag_id)

Or even ids of tags

 ArticlesTag.all.group_by(&:tag_id).keys

That give me ids of the tag so to get names need to pull it from tag model in the loop.

tags =  ArticlesTag.all.group_by(&:tag_id).keys
tags.each do |key|
 puts Tag.find(key).name
end

Is there a better - easier way to get tags names ?