【问题标题】:how to seed multiple id's for one parameter under a has_many through association如何通过关联在 has_many 下为一个参数播种多个 id
【发布时间】:2016-03-23 23:04:47
【问题描述】:

一段时间以来,我一直在为这个应用程序的建模和初始种子过程苦苦挣扎。我想在EventArtist 之间制作一个联合表EventArtist,这样就可以检索任何活动阵容中的所有艺术家以及艺术家参加的所有活动。如何添加多个artist_ids 到一个事件?

这是种子文件:

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

【问题讨论】:

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


    【解决方案1】:

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

    首先,我们需要声明一个 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
    

    【讨论】:

    • 这听起来应该可以,但我现在收到此错误syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' has many :artists, through :event_artists
    • 哦等等,那一定是因为我在模型文件中缺少:
    • 忘了提到您还需要将 has_many :event_artists 添加到 Event。编辑了我的答案。
    • undefined method 'event_artist' :(
    • 我想我知道问题出在哪里,会玩并给出更新
    【解决方案2】:

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

    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
    

    两者都可以。祝你好运;)

    【讨论】:

    • EventArtist 是如何参与其中的呢?
    • 您需要将 has_many :event_artists 添加到两个模型中。我编辑我的答案
    【解决方案3】:

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

    您还需要在创建任何EventArtists 之前创建Event

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      相关资源
      最近更新 更多