multiselect check_boxes with activeadmin rails 4.0.3 - activeadmin

I have two models sessions and speakers, I need to add multiple speakers to sessions. for that am using has_many belongs_to association
my models are
class Speakers < ActiveRecord::Base
has_many :sessions
end
class Sessions < ActiveRecord::Base
belongs_to :speakers
end
I am using activeadmin for this project, how to add multiple ids to speakers_id column in sessions table via check_boxes in activeadmin.
thanks

For this goal I wouldn't use has_many association.
The best solution would be to use has_many through and join table.
That would save you time.
Otherwise you will have to write code for recording comma separated values in one column, since this is not common logic for formtastic. That is also might cost you problems during update action.

Related

Split Model declaration among several app in Django

I am working and building my website with Django and I am facing this logical issue.
My project is made by several app.
I would like to declare in each one of these a "piece" of a bigger model that will be represented in one single table-
Example:
model Person
model DetailsPerson
As each single app specifies a specific part of the person, my idea was to decentralize the declaration of DetailsPerson model so that they figure in one single table but each app enlarge the fields the app needs to work.
Is this possible?
EDIT 25/11/2021: here is a graphical representation of how I would like my models be working like
I would like to declaire, "to detail" the table Person adding in various app the fields the app itself introduce. In this way I can have a single table with various fields, introduced time by time as I create new apps into my project.
Is this possible? My aim is to keep only one table.
I tried with Nechoj's first solution but declairing Person(models.Model) and then in another app PsysicalModel(Person) adding field_1 and field_2 and then makemigrations and migrate doesn't fill my table with field_1 and field_2 but leaves the table with only id_person, birthdate and city.
Class Inheritance
You can use class inheritance, like
class Person(models.Model):
(fields)
# in other file, import this class an inherit
class PersonWithDetails(Person):
(add. fields)
EDIT: According to the docs on multitable inheritance this will create an additional table for PersonWithDetails that holds the additional fields. However, to the user it appears as if all data is stored in a single table. For example, filterand update queries work as expected:
PersonWithDetails.objects.filter(<some criteria>)
will return instances that contain all fields (both from Person and PersonWithDetails) as if all fields where stored in a single table PersonWithDetails. Furthermore, it is possible to select all persons irrespective of their details:
Person.objects.all()
will return all Person instances, including those that have been created as PersonWithDetails. If you have a Person instance p at hand, then you can check whether a special attribute is present and then know, whether this instance is also a PersonWithDetails:
if p.personwithdetails is not None:
p.personwithdetails.field_1
This example show how to acces fields of the PersinWithDetails if the instance at hand is Person.
OneToOneField
Another option is to use one-to-one relations.
class Person(models.Model):
(fields)
# in other file, import this class and do
class DetailsPerson(models.Model):
person = models.OneToOneField(Person, on_delete=models.CASCADE)
(additional fields)
In case of adding details to a Person class, I would prefer the second option.
ForeignKey
And if you want to have several different Detail classes to one Person, use ForeignKey:
class DetailsPerson1(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
(additional fields)
class DetailsPerson2(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
(additional fields)

Is there is any support for Single Table Inheritance in Prisma?

I have one project in Rails where I used Single Table Inheritance on Users Table, by creating two roles for Users table - 1.Clinician and 2.Patient. The model desc. is below
class Patient < User
has_many :clinician_patients
has_many :clinicians, through: :clinician_patients
end
class Clinician < User
has_many :clinician_patients
has_many :patients, through: :clinician_patients
end
Here I have another table clinician_patients :-
class ClinicianPatient < ActiveRecord::Base
belongs_to :clinician
belongs_to :patient
end
I am new to Prisma and I want to use STI and relationships like Rails in Node using Prisma. How should I use STI in prisma models?
As you can see here prisma does not support table inheritance yet, but support is planned in the future.
There are some shim implementations you can try out until there is an official implementation:
https://github.com/smcelhinney/prisma-merge-schema
https://github.com/amplication/prisma-schema-dsl

Jhipster - generate a CRUD front end for a parent entity and its children/relationships on one page?

The use case I am thinking of is where you a parent entity "X" that has one or many child entities "Y" and "Y" has a "many to one" relationship to an entity "Z".
I suppose a common example of this would be "An order contains one or more orderItems and each orderItem has a product". This isnt the use case, but the relationships are similar.
Is it possible with j-hipster or blueprints to generate a front end page that lets you create a full parent "X" and its children all on the same page/for?
This project generator-jhipster-x already has the back-end functionality to manage Parent-Child relationships. Front-end functionality is still work in progress.

How can I create an ActiveAdmin filter that will show only objects without a value specified?

I am using the latest ActiveAdmin and I am trying to create a filter for a model that has a belongs_to relationship with another model, and therefor has a column litigation_canonical_docket_event_id that refers to that model.
How can I create a filter that will show only objects that do (or do not) have a value in that ID column?
You can use activeadmin's collection attribute with proc to customize your query. For example:
filter :your_model_field, :collection => proc { YourModel.where("litigation_canonical_docket_event_id IS NOT NULL") }
I ended up creating a standard model scope, then making that scope ransacakable, then referring to it as a filter. The above comment might work if I wanted to find all items that have a specific value but what I am after is finding whether it has any value (or not).

How to selectively populate waterline associations via query param in sails.js

By default, sails will populate all relationships within a model when it's corresponding API route is hit. Does anyone know if it's possible to toggle this functionality? If I'm working with a one-to-many association, I may not want to populate the association when doing a listing of all items for performance reasons. But when viewing a single item, it would be nice to complete the population.
For example, say one ticket can have many comments. I don't care about the comments when fetching a case listing but would be important when viewing a specific case. I took a guess at how it could function but it fails:
localhost:1337/tickets?populate=false
Update
I implemented the above functionality within balderdashy/sails#1695. The only change is that you selectively choose which associations to populate using:
localhost:1337/tickets?populate=[] // Don't populate anything
localhost:1337/tickets?populate=[comments] // Only populate comments
This would override whatever is defined for populate within your blueprint config.
You just need to separate your assosiactions via comma, just like this:
localhost:1337/tickets?populate=comments,owner&SOME_OTHER_PARAMS

Resources