sqlite3 - How to show multiple ids to a table in rails -


i making band application venue has many events , bands through events.

i realized in form creating event can hold 1 band_id want have many bands because makes sense so.

this schema

activerecord::schema.define(version: 20170817180728)    create_table "bands", force: :cascade |t|     t.string "name"     t.string "genre"     t.string "image"     t.boolean "explicit_lyrics"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    create_table "events", force: :cascade |t|     t.string "name"     t.text "date"     t.boolean "alcohol_served"     t.string "image"     t.integer "venue_id"     t.integer "band_id"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    create_table "venues", force: :cascade |t|     t.string "name"     t.string "city"     t.string "state"     t.boolean "family_friendly"     t.string "image"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end  end 

these models

class venue < applicationrecord     has_many :events     has_many :bands, through: :events end  class event < applicationrecord     belongs_to :venue     belongs_to :band  end  class band < applicationrecord     has_many :events end 

i new rails practice web app. want able able show multiple band_ids event.

would keep repeating t.band_id in form??

you'll want specify foreign key relationship in migration reflects active record associations you've set in models using belongs_to instead of data type. way, you'll references 1 table another, or in case, 1 table 2 others, how 1 table 2 one-to-many relationships set up.

class createevents < activerecord::migration   def change     create_table :venues |t|       t.string :name       t.string :city       t.string :state       t.boolean :family_friendly       t.string :image       t.timestamps     end      create_table :bands |t|       t.string :name       t.string :genre       t.string :image       t.boolean :explicit_lyrics       t.timestamps     end      create_table :events |t|       t.belongs_to :venue, index: true        # here!       t.belongs_to :band, index: true         # , here!       t.string :name       t.text :date       t.boolean :alcohol_served       t.string :image       t.timestamps     end   end end 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -