0
我有兩個表emp
和requisition
從軌道中的兩個表中獲取數據?
emp.rb
class Emp < ActiveRecord::Base
has_many :requisitions
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:confirmable,:lockable
attr_accessor :email
attr_accessible :password,:password_confirmation,:remember_me, :email , :username
string_regex = /\A[a-z]+\z/i
validates :username,
:format => { :with => string_regex ,:message=> "must be string"},
:uniqueness => {
:case_sensitive => false
}
end
Requisition
class Requisition < ActiveRecord::Base
belongs_to :emp
attr_accessible :name , :unit , :quantity ,:from ,:particular ,:total , :cost
validates :name , :unit , :quantity ,:from ,:particular ,:total , :cost , presence: true
validates :particular, :presence => true,
:length => { :within => 6..60 }
validates :quantity, numericality: { only_integer: true },
:length => {:maximum => 4}
validates :quantity, numericality: { only_integer: true },
:length => {:maximum => 4}
end
如果我要算EMP的徵用,我應該要做什麼編輯的數量?
在我的日誌
irb(main):002:0> @emp.requisitions.count
(1.0ms) SELECT COUNT(*) FROM "requisitions" WHERE "requisitions"."emp_id" =$1 [[nil, 2]]
PG::UndefinedColumn: ERROR: column requisitions.emp_id does not exist
LINE 1: SELECT COUNT(*) FROM "requisitions" WHERE "requisitions"."e...: SELECT COUNT(*) FROM "requisitions" WHERE "requisitions"."emp_id" = $1
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column requisitions.emp_id does not exist
LINE 1: SELECT COUNT(*) FROM "requisitions" WHERE "requisitions"."e...
^
: SELECT COUNT(*) FROM "requisitions" WHERE "requisitions"."emp_id" = $1from F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:811:in `prepare
我已添加我的日誌.. –
@SNEHPANDYA您忘記了遷移。我擴大了我的答案。 –
完美答案.. –