ActiveAdmin -- How to create join table records? - ruby-on-rails

I have two categories merchants and aliases who have a join table called aliases_merchants
In ActiveAdmin, how can I display a textbox whose content will get entered into the joining table?
I'm able to display the textbox but upon post it doesn't create the record in the joining table in the db.
Ideally, I would love to be able to get the functionality like the following (but I feel ActiveAdmin wouldn't be able to do it)...

I solved this by creating a sidebar and two member functions in the ActiveAdmin resource to accomplish this. Works just described above.

Related

Ruby on Rails: Nested form with Existing Objects

I want to create a nested form in Ruby on Rails. Let's say I have a Model Player and a Model Achievement.
In a form for the User I want to assign n Achievements to the User. The Achievements have been created before, so they exist when the Achievements get assigned. How do I do that? How should the View, the Controller and the Models look like? I searched for hours but I didn't find anything.
I'd like to realize it with a select field, with which the Achievements get selected.
there is quite a few examples out there and depending which form are you using you can choose:
https://stevepolito.design/blog/create-a-nested-form-in-rails-from-scratch/
https://levelup.gitconnected.com/the-many-things-about-nested-form-in-ruby-on-rails-15f32ed66446
https://tudip.com/blog-post/the-implementation-of-nested-forms-in-ruby-on-rails-fields-for/#:~:text=Rails%20provide%20a%20powerful%20mechanism,the%20parent%20on%20associated%20records.
you can also use simple_forms https://github.com/heartcombo/simple_form which will automatically create a select field for the achievements (you can check their documentation) and this might help How should I use rails and simple_form for nested resources?

Octopus gem dynamically select shard for an associated model based on shard_id

We have a users table. Users have many listings.
We'd like to shard the association model Listing such that all users stay on database "master" shard. Users will get a shard_id column and listings will be split into different databases "shard1", "shard2".
We can augment our code to access the listings on the correct shard using the using method:
Listing.where(user: current_user).using(current_user.shard_id)
However that is a big code change. Ideally we want to just keep using our existing association statements like this:
current_user.listings
And have it automatically use current_user.shard_id beneath the hood.
Any suggestions for doing this?
According to the documentation, current_user.listings should work out of the box.
Octopus also handles associations. When you try to get a object that is associated to another object, you could use normal ActiveRecord syntax to get the objects
https://github.com/thiagopradi/octopus/wiki/How-Octopus-Works

Polymorphic join table and nested forms

I don't know what's the best way to solve this problem and I'll appreciate your help.
This is what I want to achieve:
There is an Act model. And it has many Organizations.
An Organization can have many Groups and also, a Group can have many Subgroups.
The Groups are loaded dynamically depending on the selected Organization, and the Subgroups are also loaded depending on the selected Group.
An Organization could have a Group or not, and also a Group could have a Subgroupor not
Here you are a mock-up of what I have explained above:
In the new Act form there is a section to add Organizations, something like this:
When you write an Organization it loads the options for the Group selector, and the same happens with a selected Group and the Subgroup selector:
When you click the add button, a new Organization section appears
A field can be empty. (This is a valid form)
Also, If you want to edit the Act, the form should show the Organizations added before:
I have this DB:
ActOrganizations is a polymorphic table, and also a join table
My main problems are about the form: how to build it to retrieve and persist the data easily and how to retrieve the stored Organizations to show them in the edit form. (I'm using simple-form)
I tried many approaches, but they were not very elegant. I'd like to know the closest way to the "rails-way" to do this.
Thank you very much for your time

Create group of rows in rails

I have a simple rails application with a table created using the rails generate scaffold command. It allows the user to add the names of different movies using a form i.e. automatically generated by rails. I want to add an option where a User can add the name of a director like Christopher Nolan and then add the name of all the movies of that director within it, sort of like a group in the rows. Also all the subrows need to be linked to each other. Is this possible on rails?
P.S. I know that Stack Overflow works on the concept where I add a snippet of code for help, but since I'm a newbie I don't have any idea about how to even start with this
About nested model form http://railscasts.com/episodes/196-nested-model-form-part-1
And this gem can help u for create form with list items
https://github.com/nathanvda/cocoon

Dropdown Box Ruby on Rails question

I am new to rails so go easy. I have two tables that I am trying to work with here, 'post' and 'category'.
The 'post' table includes the following columns, title:string content:text category:string.
The 'category' table simply contains name:string.
The idea is that the client can manage the categories and also when adding a new post, they can select from a drop down that references their categories.
What is the best way to accomplish this?
You might want to model the category differently. The usual approach is to create a PostCategory model and controller, and use a relation from posts to PostCategory. Read up on belongs_to and the other rails associations before you get much further into this project. When you're ready to continue, take a look at formtastic, it makes handling the forms for the associations much easier to code
flyfishr64 is right, the "correct" way to do this would be to put the categories in their own model/table.
There's lots of helpers like collection_select that will take your list of categories (PostCategory.all) and make a dropdown list for you with the appropriate name to save it in a specific field.
That said, you could pull a distinct list of the entries in that column already and use that for your dropdown, but it's a lot more hassle than just making a model for the category.

Resources