ControllerNameController#create SQLite3 :: SQLException中的ActiveRecord :: StatementInvalid:无此类列:

MulleOne

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

  • “在SongsController#create SQLite3 :: SQLException中的ActiveRecord :: StatementInvalid:没有这样的列:songs.artist:从“歌曲”中选择1个(在“歌曲”中(“歌曲”。“名称” =“还有更多”和“歌曲”。)艺术家”(IS NULL)限制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

结尾

artist_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

 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"]
MulleOne

通过将包含artists_songs_attributes[:id, artist_id, song_id]song_params方法songs_controller,可以通过连接表artist_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]需要将其传递到联接表(artist_song)中,以创建实际创建之间的链接的行。两个表格(artistsong)中创建的行

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档