vendredi 8 mai 2015

Rails has_many api interaction

I'm adding a new api interaction to a rails app. So, for this example, I have two models: a bag, and a thing. A bag can have many things. In my bag model, I have

has_many :things, :class_name => 'Api::Things'

and in my things model, I have

belongs_to :bag, :class => 'Api::Bag'

In my api (written in Go) I have

// Represents the json object that is returned to the end user when a request is made
type Thing struct {
    Id              int     `db:"id" json:"id"`
    Name            string  `db:"thing_name" json:"thing_name"`
    ThingTypeId     int     `db:"thing_type_id" json:"thing_type_id"`
}

type ThingCollection struct {
    Data []*Thing `json:"things"`
}

func (m *ThingCollection) List(bag_token string) error {
    sql :=
        `SELECT
            ifnull(t.id,0) as id,
            ifnull(t.thing_name,"") as thing_name,
            ifnull(t.thing_type_id,0) as thing_type_id
        FROM ` + dbNames.fakeDB + `.things t
        JOIN ` + dbNames.fakeDB + `.bags b ON b.id = t.bag_id
        WHERE b.token = '` + bag_token + `'`

    return mysql.DB.Select(&m.Data, sql) --the mysql is defined in an import
}

I want my things to be populated when I make the api call to return a bag. This is not happening when I make that call in the bag controller. Do I need some kind of def self in the thing model? Am I missing something else?

Aucun commentaire:

Enregistrer un commentaire