Special Case Models in ActiveRecord Schema (Rails) - ruby-on-rails

I have a project that is built with a tagging model to reference three different models: artist, article, event. I have associated each model to the tagging model via has_many through:. I have two problems; both related to each other:
I have 5 "default" methods that I wish to be able to call restfully from the tagging controller/model: popular, upcoming, events, articles, and artists. Each method is designed to do exactly as it's name implies. The issue lies within these methods being a query by nature. How can I maintain a consistent schema where 5 of the many taggings models I have require a special attribute (let's call it content) that should subsequently call the appropriate method?
When calling any one of the taggings models, it will return (in it's content attribute) an array of the 3 models specified earlier. Other than adding a method within each model that contains a localized description of the model and then calling upon that type method to match another hardcoded string elsewhere, is there any alternative? I don't like how the implementation I just described requires me to hardcode values. Eek.
I'm very new to Ruby on Rails, so I apologize if this is an obvious solution. However I have spent a week looking into ways to solve this compound problem that I'm trying to solve. Any input is appreciated!

As far as i unterstand you need a polymorphic association. Because tags can be applied on different models, the clue is to treat all these models in polymorphic manner by marking them as :taggable on the association to tags.
I would just use the popular acts_as_taggable_on gem for it.

Related

has_many polymorphic relationship where our _type field is an int?

We have an app in Rails 4 (but soon to be Rails 5) that has a polymorphic log class that we inherited from a Django app.
In a traditional polymorphic relation, the objects would have source_type and source_id, which would be things like 'some_class' and 123.
In our little model, we have content_type and content_id which unfortunately map to two ints, where the int in content_type further points to a ContentType model that -- to further complicate things -- dont map to Rails-ey types ('some_type' but rather django-ey 'some type class'.
It's a mess.
Is there any way to override the value used in looking in a polymorphic association (e.g. instead of using 'some_class', use 11) -- but just for the association? There is an answer for a problem somewhat similar to ours, but it is 4 years old and it overrides association_class. We still use some of this strange Django voodoo elsewhere in our codebase, so overrides feel like they'd cause more problems than solve.

ActiveAdmin: Filter by count of child objects

In a Ruby on Rails app that heavily relies on ActiveAdmin, I have a Sponsor model, which is associated with a Sponsorship model. One sponsor can sponsor many children, so one sponsor can have many sponsorships.
What I would like to do, is to be able, on the Sponsor index page, to filter out sponsors by the number of sponsorships they have. So that, for example, I want to see only those sponsors who have more than one sponsorship, or less than five, and so on. You get the idea. In Ruby-speak, I want a filter that would do something along the following lines:
Sponsor.all.select { |sp| sp.sponsorships.count > 1 }
I found out that it is, in fact, quite hard to do. Default ActiveAdmin’s filters work on attributes of a particular model (or its children models), and not on custom methods, while I need to filter precisely by a custom method. So this is the filter in ActiveAdmin’s combined view/controller sponsor.rb file:
filter :sponsorships_count, label: 'Sponsorships', as: :numeric
where :sponsorships_count is not an attribute of the Sponsor model.
I tried to use Ransacker (it seems some people have had success with it), but couldn't figure out the proper syntax. Others had some luck specifying filters as custom (using as: custom syntax such as here and providing the name of a model scope as a filter name), but this didn't work for me (the app is using ActiveAdmin version 1.0.0.pre which is reported not to work with this approach).
Help, somebody?

Rails: Get all associated models from has-many association

I need to get all associated models from the other associated model, on which I want to run query first.
For example, I got Post model and Tag model. I need to get all Posts, which associated with some Tags.
There's no problem, if I have only one Tag – just call 'tag.posts', but if I have more, then one Tag – for example, I need to do somethink like:
Post.where(id: PostTag.where(tag_id: some_ids).pluck(:category_id).uniq)
I belive that Rails have a built-in solution. So, anybody knows it?
My thought is:
Post.joins(:post_tags).where('post_tags.tag_id' => some_ids).uniq
You can make it a scope for easier reuse. I don't think there is a built-in method for this situation.

Rails blog and post linking

I have a rails app that works almost like a blog, and I use a tagging system to categorize the posts.
I need to add to some of the posts something similar to a "related posts" feature.
So for example if post 1 is related to post 4, at the end of the show action for post one I want to render an image of post 4 and at the same time at the end of post 4 an image of post 1.
My idea is to create a "link" model that has a HABTM relations with the post model, but I'm not sure if a "post" has many "links" trough "linkings" would be better.
Both of the ideas seem to have the same result, so which approach should I prefer?
HABTM is by nature very simple, with just a table of foreign key pairs joining models.
Typically has_many through is used when you need to add additional attributes to that join relation, and/or when you need to treat the joins as their own model.
In your case, for example, you might want the links to appear in the order that they were created. For this to happen you'd need to store the create timestamp on the relationship. For this, the simple HABTM join table is not enough, so you switch to has_many through and create a Linking model to encapsulate the join.
To continue the example, you might also make Linking a first-class resource, and have a page where you can edit/add/remove them separately from either linked Post.
Personally I've always used has_many through in the majority of cases. It just feels cleaner to me (no auto-naming table magic to accept or override, and the linking is more visible), and I find that very often, join relationships do deserve to be first class citizens.

Associations of Hierarchical Objects, belongs_to, has_many of Own Class

I'm trying to make objects belong to other objects of the same class by adding a parent_id attribute to the object so active record can associate them with each other in a hierarchical manner.
I know I can just write an instance method to do a find and get them, but I want to know if there is a more rails appropriate way to accomplish this, so can I set this up with active record associations, if so how?
It seems that this gem may be of help :
https://github.com/skyeagle/nested_set
or acts_as_tree:
https://github.com/rails/acts_as_tree
There are a lot of ways of nesting data in databases. The one to choose manly depends on how you are going to access this data, and how often you are going to change the tree.
Here is a list of current nesting plugins for rails: http://www.ruby-toolbox.com/categories/activerecord_nesting.html Be sure to reed each of the gems documentation in order to choose the most appropriate one for your situation.

Resources