嗨,我正在实施一个项目。在这个 Loan_fines 表中引用了两个表,即书籍和类别。
我收到以下错误:-
NoMethodError in LoanFinesController#create
undefined method `blank' for [1, 1]:Array
app/models/loan_fine.rb:32:in `book_or_category'
app/controllers/loan_fines_controller.rb:46:in `block in create'
app/controllers/loan_fines_controller.rb:45:in `create'
我的贷款罚款表如下:-
id integer NOT NULL DEFAULT nextval('loans_fines_id_seq'::regclass),
category_id integer,
book_id integer,
loan_duration integer,
fine_amount numeric(8,2) DEFAULT 0.0,
fine_duration integer,
deleted integer NOT NULL DEFAULT 0,
CONSTRAINT loans_fines_pkey PRIMARY KEY (id)
这张表是我学长给的,不知道是什么意思,nextval('loans_fines_id_seq'::regclass)
为什么用
我的创建贷款罚款控制器如下:-
def create
@loan_fine = LoanFine.new(params[:loan_fine])
respond_to do |format|
if @loan_fine.save
format.html { redirect_to @loan_fine, notice: 'Loan fine was successfully created.' }
format.json { render json: @loan_fine, status: :created, location: @loan_fine }
else
format.html { render action: "new" }
format.json { render json: @loan_fine.errors, status: :unprocessable_entity }
end
end
end
我的贷款罚款模型如下:-
class LoanFine < ActiveRecord::Base
attr_accessible :book_id, :category_id, :loan_duration, :fine_amount, :fine_duration
# ------- ASSOCIATIONS -----------
belongs_to :book
belongs_to :category
# ------ VALIDATIONS ------
validate :book_or_category
private
def book_or_category
if [self.book_id, self.category_id].compact.blank.size == 0
errors[:base] << ("Please choose either book or category.")
end
end
end
我被困在这个我不知道如何实现这个。谁能帮我