ControllerNameController中的ActiveRecord :: StatementInvalid #create SQLite3 :: SQLException:没有这样的列:

时间:2016-04-29 01:18:36

标签: ruby-on-rails ruby activerecord sqlite

我有一个“歌”和“艺术家”模型,我通过has_many加入:通过关系。在点击“创建歌曲”时,我得到了这个持续的错误:

  • 在SongsController #create中的ActiveRecord :: StatementInvalid SQLite3 :: SQLException:没有这样的列:songs.artist:SELECT 1 AS one 来自“歌曲”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(Song,Artist,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"]

1 个答案:

答案 0 :(得分:0)

通过将artists_songs_attributes[:id, artist_id, song_id]包含在songs_controller中的 song_params方法中,可以成功添加一首具有所需多对多关系的歌曲<加入表格中的strong>歌曲和艺术家 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 )创建实际在两个表的创建行之间创建链接的行(艺术家&amp; 歌曲)。