我正在尝试将用户用户名添加到song
。
应用/模型/ user.rb
class User < ActiveRecord::Base
has_many :songs
...
end
应用/模型/ song.rb
class Song < ActiveRecord::Base
belongs_to :user
...
end
我正在尝试迁移以向我的歌曲表添加用户名表
class AddUsernameToSongs < ActiveRecord::Migration
def change
add_column :songs, :username, :string
end
end
但是当我尝试运行rake db:migrate
时,我一直收到一个未初始化的常量错误我希望能够在每首歌曲上播放
<%= song.username %>
发布该曲目的作者。
我正在使用设计来设置我的用户,而设计表已经有了一个用户名字段。
答案 0 :(得分:1)
最好通过ID将歌曲链接到用户,而不仅仅是用户名。
如果你还没有这个。我认为你按照自己的意愿行事。
#include<stdio.h>
#define MAX 5
#include<string.h>
char arreglo[MAX];
int indice;
//prototipo de funcion
int menu (char texto[],int n);
void insertar (char dato);
void listar();
main(){
//variable locales
int opcion;
char dato[50];
indice = -1;
do{
opcion = menu("\n1) Insertar\n2) Borrar\n3) Actualizar\n4) Ordenar\n5) listar\n6) Buscar\n7) Salir\n",7);
switch(opcion)
{
case 1://insertar
if(indice < MAX-1){
printf("Dame el dato a insertar");
scanf("%s", dato);
insertar(dato[50]);
}//fin if
else{
printf("error no hay espacio" );
}//fin else
break;
case 2:
printf("\nBorrar");
break;
case 3:
printf("\nActualizar");
break;
case 4:
printf("\nordenar");
break;
case 5://listar
printf("\nListando\n");
listar();
printf("%s \t ", arreglo);
break;
case 6:
printf("\nBuscar");
break;
case 7:
printf("\nHasta luego...");
break;
}//fin switch
}while(opcion != 7);
}//finn del main
int menu (char texto[], int n){
int opcion;
do{
printf("%s", texto);
scanf("%d",&opcion);
if(opcion <1 || opcion >n){
printf("error: opcion no valida");
}//fin del if
}while(opcion <1 || opcion >n);
return opcion;
}//fin funcion
void insertar (char dato){
indice ++;
arreglo[indice]= dato;
}//fin funcion
void listar(){
char i;
for(i=0; i<=indice ; i++){
printf( "%s \t ", arreglo[i]);
}
printf("\n");
}//fin funcion
在您的歌曲模型中,您可以添加名为** author **
的方法class AddUserToSongs < ActiveRecord::Migration
def change
add_column :songs, :user_id, :integer
end
end
现在,在您的观看中,您可以执行以下操作。
class Song < ActiveRecord::Base
belongs_to :user
...
def author
user.name # this can be username or what ever field in the user's table you want
end
end
我希望这会有所帮助。 快乐黑客