vendredi 1 décembre 2017

Rails 5: 3-Model Setup for Standard has_many and belongs_to (collection_check_boxes)

The concept behind this app is an 'association app' so to speak. A user can go in and create new relationships between outages and children. This is done via the collection_check_boxes method.

Normally you'd just have 2 tables when doing a simple has_many and belongs_to. You'd usually only have 3 models if you are doing a has_and_belongs_to_many

class Outage < ApplicationRecord
    has_many :relationships
    has_many :children, through: :relationships
end

class Relationship < ApplicationRecord
    belongs_to :outage, optional: true
end

class Child < ApplicationRecord
end

In outages_controller.rb I have child_ids permitted (this is for collection_check_boxes).

def outage_params
    params.require(:outage).permit(:heat_ticket_number, :child_ids => [])
end

A child can only belong to one outage at a time.

A child does not need to belong to an outage. It can exist alone.

I need the association model (relationship.rb) to be separate from child and outage, bc child and outage will connect to two separate remote SQL Server databases that will just read data. Where the relationship model will make the association: child belongs_to outage, outage has_many children.

I cannot update the remote SQL Server tables to handle the association. Hence only reading data for Outage and Child.

The current issue:

When I go to update an outage with children using collection_check_boxes, this is what I'm seeing in my server log (this is if I update outage with ID of 5 having child_ids of 1, 2, and 3:

enter image description here

In my schema.rb I have the following for relationships:

create_table "relationships", force: :cascade do |t|
    t.integer "outage_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "child_id"
    t.index ["child_id"], name: "index_relationships_on_child_id"
    t.index ["outage_id"], name: "index_relationships_on_outage_id"
end

So as you can see child_id is not being passed into the INSERT statement in the screenshot.

What am I overlooking here?




Aucun commentaire:

Enregistrer un commentaire