我创建了一个播放列表关联,以便用户可以选择将曲目添加到播放列表。这样做的问题是,用户现在无法通过belongs_to :user
我协会中的某些东西打破了关系,我无法弄清楚它是什么,这里是我模特的所有相关代码:
class User < ActiveRecord::Base
require 'soundcloud'
acts_as_voter
belongs_to :gallery
has_many :tracks
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
has_many :playlists
has_many :tracks, through: :playlists
end
-
class Playlist < ActiveRecord::Base
belongs_to :user
has_many :playlist_tracks
has_many :tracks, through: :playlist_tracks
end
-
class PlaylistTrack < ActiveRecord::Base
belongs_to :playlist
belongs_to :track
end
-
class Track < ActiveRecord::Base
acts_as_votable
belongs_to :user
has_many :playlist_tracks
has_many :playlists, through: :playlist_tracks
mount_uploader :track, TrackUploader
validates :title, presence: true
validates :description, presence: true
validates :track, presence: true
validates_length_of :title, :minimum => 2, :too_short => "please enter at least %d character"
validates_length_of :description, :minimum => 10, :too_short => "please enter at least %d characters"
def self.search(query)
# where(:title, query) -> This would return an exact match of the query
where("title || description like ?", "%#{query}%")
end
end
如果我应该澄清有关这个问题的任何事情,请告诉我。谢谢。
编辑:
错误:无法修改关联'User#tracks',因为它会经历多个其他关联。
错误指向tracks_controller.rb中的create动作
class TracksController < ApplicationController
skip_before_filter :verify_authenticity_token
def show
@track = Track.find_by(id: params[:id])
end
def new
@user = User.find(params[:user_id])
@track = Track.new
end
def create
@user = current_user
@track = @user.tracks.create(track_params)
if @track.save
redirect_to user_path(current_user)
else
redirect_to :back
flash[:alert] = 'There was an error processing your request'
end
end
def upvote
@track = Track.find_by(id: params[:id])
@track.upvote_by current_user
render json: @track
end
def unvote
@track = Track.find_by(id: params[:id])
@track.unliked_by current_user
render json: @track
end
def likes
@user = User.find_by(id: params[:id])
@likes = @user.find_voted_items
render json: @likes
end
private
def track_params
params.require(:track).permit(:track, :user_id, :title, :description, :artist)
end
end
更新:当我在用户模型中删除7时,它不会给我一个错误,但该曲目不会保存......