我有两种模式:
class PhonebookEntry {
private Name name;
private PhoneNumber phonenumber;
public PhonebookEntry(Name name, PhoneNumber phonenumber) {
this.name = name;
this.phonenumber = phonenumber;
}
public Name getname() {return name;}
public PhoneNumber getnumber() {return phonenumber;}
public void call() {
if (phonenumber.isTollFree()) {
System.out.println("Dialing (toll-free) "+name+": "+phonenumber);
}
else
System.out.println("Dialing "+ name.getFormal()+ ": ("+phonenumber.getAreaCode() +")"
+ phonenumber.getExchange()+"-" + phonenumber.getLineNumber());
System.out.println();
}
public String tostring() {return name +":"+ phonenumber;}
public static PhonebookEntry read(Scanner scanner) {
if(!scanner.hasNext()) return null;{
Name name = new Name(scanner.next(), scanner.next());
PhoneNumber phonenumber = new PhoneNumber(scanner.next());
return new PhonebookEntry(name,phonenumber);
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("phonebook.text"));
PhonebookEntry prevEntry =null;
int count =0;
PhonebookEntry entry = (read(scanner));
while(read(scanner) != null) {
if(prevEntry != null) {
if(prevEntry.equals(entry))
System.out.println("Duplicate phone book entry"+ '"'+entry.name+": "+ entry.phonenumber+'"'+ "discovered");
else if(prevEntry.name.equals(entry.name) && !prevEntry.phonenumber.equals(entry.phonenumber)) {
System.out.println("Warning duplicate name encountered " + '"' + entry.name +":"+entry.phonenumber+ '"' + " discovered");
}
}
count++;
System.out.println( "phone book entry: "+entry.name + ": "+ entry.phonenumber);
prevEntry = entry;
entry.call();
}
System.out.println( count+" phonebook entries processed.");
scanner.close();
}
}
class Author < ApplicationRecord
has_many :books
end
class Book < ApplicationRecord
belongs_to :author
end
有Book
。
是否可以在未指定author_id
的情况下创建Book
,或者是否需要此属性?
错误:author_id
答案 0 :(得分:5)
默认情况下需要使用rails 5.除非你指定它不是
class Book < ApplicationRecord
belongs_to :author, optional: true
end