Reputation: 450
I am working on a react native application and as part of the project I am building an API using Ruby on Rails 5 in API Only mode with ActiveAdmin for the admin interface. I have everything setup, and it's all working well.
I was given the data in a file before having the idea of an API, the structure of which I need to replicate using rails, this is so it can be content manageable.
But I can't seem to get the active record associations correct.
This is a sample of the data file...
{
name: 'Scottish Stoics',
days: [
{
day: 'Monday',
meeting: [
{
name: 'Meeting one',
location: 'Test',
startTime: '11.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting two',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting three',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
],
},
{
day: 'Tuesday',
meeting: [
{
name: 'Meeting one',
location: 'Test',
startTime: '10',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting two',
location: 'Test',
startTime: '10',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting three',
location: 'Test',
startTime: '10',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
],
},
],
},
{
name: 'Scottish Cycling Group',
days: [
{
day: 'Sunday',
meeting: [
{
name: 'Meeting one',
location: 'Test',
startTime: '11.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting two',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting three',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
],
},
{
day: 'Tuesday',
meeting: [
{
name: 'Meeting one',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting two',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
{
name: 'Meeting three',
location: 'Test',
startTime: '10.00',
address: 'Test Street',
meetingLength: '1 Hour',
city: 'Edinburgh',
},
],
},
],
}
I have a Group Model, Day Model and a Meeting Model.
Group
belongs_to :day
belongs_to :meeting
Meeting
has_many :groups
Day
has_many :groups
And I am getting this result.
[
{
id: 1,
name: "Scottish Stoics",
created_at: "2018-03-19T11:47:50.818Z",
updated_at: "2018-04-12T10:50:05.179Z",
day_id: 7,
meeting_id: 81
},
{
id: 2,
name: "Scottish Cycling Group",
created_at: "2018-03-19T11:47:57.498Z",
updated_at: "2018-04-11T22:13:26.656Z",
day_id: 7,
meeting_id: 83
},
]
Now I can use jbuilder to get the day name and meeting name which is fine, but what I can't do is select MULTIPLE meetings for one day, and MULTIPLE DAYS for the group. Is this data structure even going to be possible in Rails?
I can't work out if I should be using a different type of association, anyone able to shed some light on this for me?
Upvotes: 0
Views: 82
Reputation: 51
Assuming that the top level nesting corresponds to Group you can form the associations like that:
Day
has_many :meetings
belongs_to :group
Meeting
belongs_to :day
Group
has_many :days
has_many :meetings, through: :days
Though i have the impression that in the JSON provided it should be meetings
rather than meeting
to keep it RESTful
Upvotes: 1