我有一個forEach循環遍歷多個對象,然後在數據庫中爲每個項目創建一條記錄。雖然這確實有效,但速度很慢(有些電影有50多個演職員,這是對數據庫的很多發佈請求)。有沒有更好的方法將多個對象保存到數據庫中?如何一次將多條記錄保存到數據庫中
這是我演員控制器的創造功能,
def create
Actor.find_or_create_by(movie_id: params[:movie_id], name: params[:name], character: params[:character])
redirect_to :root
end
*更新*
登錄從我的鐵軌控制檯,
Started POST "/actors.json" for 127.0.0.1 at 2016-01-12 16:15:35 +0100
Processing by ActorsController#create as JSON
Parameters: {"_json"=>[{"name"=>"John Travolta", "character"=>"Vincent Vega", "movie_id"=>680}, {"name"=>"Samuel L. Jackson", "character"=>"Jules Winfield", "movie_id"=>680}, {"name"=>"Uma Thurman", "character"=>"Mia Wallace", "movie_id"=>680}, {"name"=>"Bruce Willis", "character"=>"Butch Coolidge", "movie_id"=>680}, {"name"=>"Ving Rhames", "character"=>"Marsellus Wallace", "movie_id"=>680}, {"name"=>"Harvey Keitel", "character"=>"Wolf", "movie_id"=>680}, {"name"=>"Eric Stoltz", "character"=>"Lance", "movie_id"=>680}, {"name"=>"Tim Roth", "character"=>"Pumpkin", "movie_id"=>680}, {"name"=>"Amanda Plummer", "character"=>"Honey Bunny", "movie_id"=>680}, {"name"=>"Maria de Medeiros", "character"=>"Fabienne", "movie_id"=>680}, {"name"=>"Quentin Tarantino", "character"=>"Jimmie Dimmick", "movie_id"=>680}, {"name"=>"Christopher Walken", "character"=>"Captain Koons", "movie_id"=>680}, {"name"=>"Rosanna Arquette", "character"=>"Jody", "movie_id"=>680}, {"name"=>"Peter Greene", "character"=>"Zed", "movie_id"=>680}, {"name"=>"Duane Whitaker", "character"=>"Maynard", "movie_id"=>680}, {"name"=>"Angela Jones", "character"=>"Esmarelda Villalobos", "movie_id"=>680}, {"name"=>"Phil LaMarr", "character"=>"Marvin", "movie_id"=>680}, {"name"=>"Steve Buscemi", "character"=>"Buddy Holly", "movie_id"=>680}, {"name"=>"Bronagh Gallagher", "character"=>"Trudi", "movie_id"=>680}, {"name"=>"Laura Lovelace", "character"=>"Waitress", "movie_id"=>680}, {"name"=>"Frank Whaley", "character"=>"Brett", "movie_id"=>680}, {"name"=>"Burr Steers", "character"=>"Roger", "movie_id"=>680}, {"name"=>"Paul Calderon", "character"=>"Paul", "movie_id"=>680}, {"name"=>"Jerome Patrick Hoban", "character"=>"Ed Sullivan", "movie_id"=>680}, {"name"=>"Michael Gilden", "character"=>"Page for Phillip Morris", "movie_id"=>680}, {"name"=>"Gary Shorelle", "character"=>"Ricky Nelson", "movie_id"=>680}, {"name"=>"Susan Griffiths", "character"=>"Marilyn Monroe", "movie_id"=>680}, {"name"=>"Eric Clark", "character"=>"James Dean", "movie_id"=>680}, {"name"=>"Joseph Pilato", "character"=>"Dean Martin", "movie_id"=>680}, {"name"=>"Brad Blumenthal", "character"=>"Jerry Lewis (as Brad Parker)", "movie_id"=>680}, {"name"=>"Lorelei Leslie", "character"=>"Mamie van Doren", "movie_id"=>680}, {"name"=>"Emil Sitka", "character"=>"Hold Hands You Lovebirds (archive footage)", "movie_id"=>680}, {"name"=>"Brenda Hillhouse", "character"=>"Mrs. Coolidge - Butch's Mother", "movie_id"=>680}, {"name"=>"Chandler Lindauer", "character"=>"Young Butch", "movie_id"=>680}, {"name"=>"Sy Sher", "character"=>"Klondike", "movie_id"=>680}, {"name"=>"Robert Ruth", "character"=>"Sportscaster #1 - Coffee Shop", "movie_id"=>680}, {"name"=>"Rich Turner", "character"=>"Sportscaster #2", "movie_id"=>680}, {"name"=>"Don Blakely", "character"=>"Wilson's Trainer", "movie_id"=>680}, {"name"=>"Carl Allen", "character"=>"Dead Floyd Wilson", "movie_id"=>680}, {"name"=>"Karen Maruyama", "character"=>"Gawker #1", "movie_id"=>680}, {"name"=>"Kathy Griffin", "character"=>"Hit-and-run Witness", "movie_id"=>680}, {"name"=>"Venessia Valentino", "character"=>"Pedestrian/Bonnie Dimmick", "movie_id"=>680}, {"name"=>"Linda Kaye", "character"=>"Shot Woman", "movie_id"=>680}, {"name"=>"Stephen Hibbert", "character"=>"The Gimp", "movie_id"=>680}, {"name"=>"Alexis Arquette", "character"=>"Man No. 4", "movie_id"=>680}, {"name"=>"Julia Sweeney", "character"=>"Raquel", "movie_id"=>680}, {"name"=>"Lawrence Bender", "character"=>"Long Hair Yuppy Scum", "movie_id"=>680}, {"name"=>"Cie Allman", "character"=>"Winston Wolfe's Girlfriend At Party (uncredited)", "movie_id"=>680}, {"name"=>"Rene Beard", "character"=>"Bar Tender (uncredited)", "movie_id"=>680}, {"name"=>"Lori Pizzo", "character"=>"Lucky Lady (uncredited)", "movie_id"=>680}, {"name"=>"Glendon Rich", "character"=>"Drug Dealer (uncredited)", "movie_id"=>680}, {"name"=>"Devan Richardson", "character"=>"Hopalong Cassidy (uncredited)", "movie_id"=>680}, {"name"=>"Ani Sava", "character"=>"Woman in Bathroom (uncredited)", "movie_id"=>680}], "actor"=>{}}
User Load (2.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "actors" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-01-12 15:15:35.503383"], ["updated_at", "2016-01-12 15:15:35.503383"]]
(11.9ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 31ms (ActiveRecord: 15.0ms)
你爲什麼不做一個會建立數據的循環,每個20或30個演員發送1個大插入查詢? – Gal
@Gal會導致1個記錄被創建?那麼每個演員的名字和角色價值將被存儲在一個記錄中?現在的結果不是多個記錄? – alucardu
SQL Server示例'INSERT INTO表(Column1,Column2)VALUES (Value1,Value2),(Value1,Value2)'這會插入多條記錄 – Malkus