2016-04-29 162 views
2

我有一个“歌曲”和“艺术家”模型,我通过has_many:through关系加入。在命中 “创造的歌曲” 我得到这个错误持续:ControllerNameController中的ActiveRecord :: StatementInvalid#create SQLite3 :: SQLException:no such column:

  • 的ActiveRecord :: StatementInvalid在SongsController#创建 的SQLite3 ::的SQLException:没有这样的列:songs.artist:SELECT 1 AS一个 FROM“歌曲“WHERE(” 诗经”, “名”= '这么多更多', “歌曲”, “艺术家” IS NULL)LIMIT 1

这里从模型代码和视图

song.rb

class Song < ActiveRecord::Base 
belongs_to :genre 

has_many :artists_songs 
has_many :artists, :through => :artists_songs 

has_attached_file :image, styles: { large: "600x600>", medium: "300x300>", thumb: "150x150#{}" }, default_url: "/images/:style/missing.png" 
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 

accepts_nested_attributes_for :artists, allow_destroy: true 
accepts_nested_attributes_for :artists_songs, allow_destroy: true 

validates :name, :presence => true, uniqueness: { scope: :artist } #Unique names in the scope of PARENT 
#4now 'featured, description, genre_id, video' is missing because they aren't really a must, the rest perhaps are 
validates :year, :lyrics, :country, :image, :presence => true 

artist.rb

class Artist < ActiveRecord::Base 
belongs_to :country 

has_many :artists_songs 
has_many :songs, :through => :artists_songs 

has_attached_file :image, styles: { large: "600x600>", medium: "300x300>", thumb: "150x150#{}" }, default_url: "/images/:style/missing.png" 
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 

validates :name, :presence => true, uniqueness: true 
validates :country_id, :image, :presence => true 

artists_song.rb “歌曲” 和 “艺术家” 之间的接合模型

class ArtistsSong < ActiveRecord::Base 
    belongs_to :song 
    belongs_to :artist 
end 

songs_controller.rb

class SongsController < ApplicationController 
before_action :find_song, only: [:show, :edit, :update, :destroy] 

def index 
    @songs = Song.all 
end 

def new 
    @song = Song.new 
    @song.artists.build 
    #@genres = Genre.all.map{|c| [ c.name, c.id ] } 
end 

def create 
    @song = Song.new(song_params) 
    if @song.save 
     redirect_to @song, notice: 'Successfully added a song.' 
    else 
     render 'index' 
    end 
end 

def show 

end 

def update 
    if @song.update(song_params) 
     redirect_to @song, notice: 'Successfully updated the song.' 
    else 
     render 'edit', notice: 'Unable to save the changes' 
    end 
end 

def edit 
end 

def destroy 
end 

#private methods/functions 
private 

#In this private method, the artists attributes are passed to be created via the Song model 
def song_params 
    params.require(:song).permit(:name, :featured, :year, :lyrics, :description, :video, :image, :genre_id, :artists_song_list, artists_attributes: [ :name, :image, :country_id ]) 
end 

def find_song 
    @song = Song.find(params[:id]) 
end 

new.html.erb(-songs_controller)

<div class="content"> 
<%= form_for @song, html: { multipart: true } do |f| %> 

    <% if @song.errors.any? %> 
     <div> 
      <%= @song.errors.count %> 
      Prevented this song from saving 
      <ul> 
       <% @song.errors.full_messages.each do |msg| %> 
        <li><%= msg %></li> 
       <% end %> 
      </ul> 
     </div> 
    <% end %> 

    <div class="field"> 
     <%= f.label :name %> 
     <%= f.text_field :name %><br> 
    </div> 

    <div class="artist-fields"> 
     <h2>Artist(s)</h2> 
     <div class="field"> 
      <%= f.fields_for :artists do |artists_for_form| %> 
       <%= render 'artist_fields', f: artists_for_form %> 
      <% end %> 
     </div> 
    </div> 

    <div class="field"> 
     <%= f.label :featured %> 
     <%= f.text_field :featured%><br> 
    </div> 
    <div class="field"> 
     <%= f.label :lyrics %> 
     <%= f.text_area :lyrics %><br> 
    </div> 

    <div class="field"> 
     <%= f.label :genre %> 
     <%= select_tag(:genre_id, options_for_select(@genres), :prompt => "Select one!") %> 
    </div> 


    <div class="field"> 
     <%= f.label :description %> 
     <%= f.text_area :description %><br> 
    </div> 
    <div class="field"> 
     <%= f.label :year %> 
     <%= f.text_field :year %><br> 
    </div> 

    <div class="field"> 
     <%= f.label :video %> 
     <%= f.text_field :video %><br> 
    </div> 

    <div class="field"> 
     <%= f.label :image %> 
     <%= f.file_field :image %> 
    </div> 

    <div class"btn"> 
     <%= f.submit %> 
    </div> 

    <%= link_to "Back", root_path %> 

<% end %> 

我什么,我做错了,如何正确地做我在rails 4中实现has_many:through关系?

COLUMN_NAMES为三个表(歌曲,艺术家,ArtistsSong)

irb(main):001:0> Song.column_names 
=> ["id", "name", "featured", "lyrics", "description", "comments", "created_at", "updated_at", "video", "year", "image_file_name", "image_content_type", "image_file_size", "image_updated_at", "genre_id"] 

irb(main):002:0> Artist.column_names 
=> ["id", "name", "created_at", "updated_at", "country_id", "—force", "image_file_name", "image_content_type", "image_file_size", "image_updated_at"] 

irb(main):003:0> ArtistsSong.column_names 
=> ["id", "song_id", "artist_id", "created_at", "updated_at"] 
+0

你是否运行过你的迁移?你的schema.rb是否反映了所需的表和字段? – Alfie

+0

@Alfie是的,我没有运行所有的迁移和计数器检查所有3个表的列名。 – MulleOne

+0

+我附加了每个表的控制台输出column_names ... – MulleOne

回答

0

通过包括artists_songs_attributes[:id, artist_id, song_id]song_params方法songs_controller,有可能成功地添加歌曲与所需许多一对多表格是通过加入表artists_song的歌曲艺术家之间的关系

因此,与其这样:

def song_params 
     params.require(:song).permit(:name, :featured, :year, :lyrics, :description, :video, :image, :genre_id, :artists_song_list, artists_attributes: [ :name, :image, :country_id ]) 
    end 

这工作:

def song_params 
     params.require(:song).permit(:name, :featured, :year, :lyrics, :description, :video, :image, :genre_id, :artists_song_list, artists_attributes: [ :name, :image, :country_id ], artists_songs_attributes[:id, artist_id, song_id]) 
    end 

我还是新的,让学习Ruby on Rails框架,因此,根据我的了解甚少,到目前为止,需要将artists_songs_attributes [:id, artist_id, song_id]传递给加入表(artists_song)以创建实际创建两个表的创建行之间的链接的行(艺术家 & song)。

相关问题