如何在has_many通过关联下为一个参数设置多个id

时间:2016-03-23 23:04:48

标签: ruby-on-rails has-many-through seeding

我已经在这个应用程序的建模和初始种子过程中挣扎了一段时间了。我想在EventArtistEvent之间制作一个联合表格Artist,这样就可以检索出任何活动的所有艺术家的阵容和所有活动艺术家参与了。如何在一个活动中添加多个artist_id

这是种子文件:

venue = Venue.create(name: "Speakeasy", address: "Lynwood Ave", zip_code: "30312")
artist = Artist.create(name: "DJ Sliink", bio: "jersey club king")
artist2 = Artist.create(name: "DJ Spinn", bio: "Teklife's chief spokesperson")
#this is probably wrong
lineup = EventArtist.create({artist_id: artist.id}, {artist_id: artist2.id})

event = Event.create(name: "Dance your ass off", 
                      date: DateTime.new(2016,2,3,10,0,0,'+7'), 
                      venue: venue, 
                      #this right here... how?
                      artist_id: , 
                      description: "free free free")

这里是架构的相关部分

  create_table "event_artist", id: false, force: :cascade do |t|
    t.integer "artist_id"
    t.integer "event_id"
  end

  add_index "event_artist", ["artist_id"], name: "index_event_artist_on_artist_id", using: :btree
  add_index "event_artist", ["event_id"], name: "index_event_artist_on_event_id", using: :btree

  create_table "events", force: :cascade do |t|
    t.string   "name"
    t.datetime "date"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "venue_id"
  end

  add_index "events", ["venue_id"], name: "index_events_on_venue_id", using: :btree

模特:

class Event < ApplicationRecord
  belongs_to :venue
  has many :artists, through :event_artists
end

class EventArtist < ApplicationRecord
  belongs_to :event
  belongs_to :artist
end

class Artist < ApplicationRecord
  has_many :events, through: :event_artists
end

3 个答案:

答案 0 :(得分:1)

这里的问题是艺术家通过创建EventArtists与事件相关联,而不是通过将artist_id添加到Event。

首先,我们需要声明一个Event有EventArtists。将has_many:event_artists添加到您的事件模型中。

在种子文件中,在创建活动时删除artist_id。

event = Event.create(name: "Dance your ass off", 
                  date: DateTime.new(2016,2,3,10,0,0,'+7'), 
                  venue: venue, 
                  description: "free free free")

您可以通过创建EventArtist

将艺术家与该活动相关联
EventArtist.create(event_id: event.id, artist_id: artist.id)

由于rails知道事件,event_artists和艺术家之间的关联,所以以下所有内容都做同样的事情:

event.event_artists.create({artist_id: artist.id}, {artist_id: artist2.id})
event.event_artists.create({artist: artist}, {artist: artist2})
artist.event_artists.create({event_id: event.id})
artist.event_artists.create({event: event})

现在你可以做像

这样的事情
lineup = event.artists

- 或 -

artist_events = artist.events

答案 1 :(得分:0)

您需要在EventArtist中为每位艺术家制作单独的记录。由于你有一个has_many通过关联,这将允许你说event.artists并返回该事件中的所有艺术家。但是每个人都需要它自己的EventArtist记录,其中一个artist_idevent_id

您还需要在创建任何Event

之前创建EventArtist

答案 2 :(得分:0)

您需要将此添加到两个模型中:

has_many :event_artists

模型如此:

class Event < ApplicationRecord
  belongs_to :venue
  has_many :artists, through: :event_artists
  has_many :event_artists
end

class EventArtist < ApplicationRecord
  belongs_to :event
  belongs_to :artist
end

class Artist < ApplicationRecord
  has_many :events, through: :event_artists
  has_many :event_artists
end

所以你可以播种:

event = Event.create(name: "Dance your ass off", 
                      date: DateTime.new(2016,2,3,10,0,0,'+7'), 
                      venue: venue, 
                      artists: [artist, artist2], 
                      description: "free free free")

event = Event.create(name: "Dance your ass off", 
                      date: DateTime.new(2016,2,3,10,0,0,'+7'), 
                      venue: venue,  
                      description: "free free free")
event.artists << artist
event.artists << artist2

两者都有效。祝你好运;)