我是Rails的新手。我有2个现有模型要建立1对1关系。
class User
has_one :info
class Info
belongs_to :user
我的问题是我是否需要为它们每个创建外键?我的最终目标是仅通过以下方式访问信息:@user.info
。
答案 0 :(得分:1)
正如评论者所说,不,你不会。仅belongs_to
端需要外键。让我为这个特定问题指向the Rails guides,但更广泛地说,它们是此类信息的绝佳资源。
答案 1 :(得分:0)
从简单开始。
import { Injectable } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Routes } from '@angular/router';
import { JhiPaginationUtil, JhiResolvePagingParams } from 'ng-jhipster';
import { UserRouteAccessService } from 'app/core';
import { Observable, of } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { ContactInfo } from 'app/shared/model/burocracy/contact-info.model';
import { ContactInfoService } from './contact-info.service';
import { ContactInfoComponent } from './contact-info.component';
import { ContactInfoDetailComponent } from './contact-info-detail.component';
import { ContactInfoUpdateComponent } from './contact-info-update.component';
import { ContactInfoDeletePopupComponent } from './contact-info-delete-dialog.component';
import { IContactInfo } from 'app/shared/model/burocracy/contact-info.model';
@Injectable({ providedIn: 'root' })
export class ContactInfoResolve implements Resolve<IContactInfo> {
constructor(private service: ContactInfoService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<IContactInfo> {
const id = route.params['id'] ? route.params['id'] : null;
if (id) {
return this.service.find(id).pipe(
filter((response: HttpResponse<ContactInfo>) => response.ok),
map((contactInfo: HttpResponse<ContactInfo>) => contactInfo.body)
);
}
return of(new ContactInfo());
}
}
export const contactInfoRoute: Routes = [
{
path: '',
component: ContactInfoComponent,
resolve: {
pagingParams: JhiResolvePagingParams
},
data: {
authorities: ['ROLE_USER'],
defaultSort: 'id,asc',
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: ':id/view',
component: ContactInfoDetailComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: 'new',
component: ContactInfoUpdateComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: ':id/edit',
component: ContactInfoUpdateComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
}
];
export const contactInfoPopupRoute: Routes = [
{
path: ':id/delete',
component: ContactInfoDeletePopupComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService],
outlet: 'popup'
}
];
PS:我真的建议从一些视频教程开始,然后重复人们在视频中所做的事情