ユーザーと製品の2つの主要なモデルを持つアプリを作成しています。ユーザーは多くの製品を所有者として持つことができ、多くの製品を借用者として持つことができます。製品の所有者は 1 人だけですが、借用者を含む多くのシーカーを持つことができます。所有プロパティには直接関連付けましたが、借用プロパティにはトランザクション モデルを作成しました。3つは次のようになります。
アプリ/モデル/transaction.rb
class Transaction
# has a seeker_id:integer, a product_id:integer and a current:boolean
before_save :check_current
# Associations
belongs_to :seeker, class_name: "User", foreign_key: "seeker_id"
belongs_to :product
# Methods
def check_current
if !self.borrowing_date.nil? && self.return_date.nil?
self.current = true
end
end
end
商品にはたくさんtransactions
の がありますが、一度に借りられるのは 1 つだけseeker
です。製品が借りられるとき、トランザクションには がborrowing_date
ありnot nil
、return_date
が ですnil
。次に、check_current
メソッドcurrent
は this のブール値をtransaction
からfalse
に切り替えtrue
ます。そのseeker
電流のtransaction
は として指定されますborrower
。
アプリ/モデル/user.rb
class User
.
.
.
has_many :owned_products, class_name: "Product", foreign_key: "owner_id", dependent: :destroy
has_many :transactions, foreign_key: "seeker_id", dependend: :destroy
has_many :requested_products, through: :transactions, source: :product
has_many :active_transactions, -> { where current: true },
class_name: 'Transaction',
foreign_key: "seeker_id",
dependent: :destroy
has_many :borrowed_products, through: :active_transactions,
source: :product
def requesting?(product)
self.transactions.find_by(product_id: product.id)
end
def request!(product)
self.transactions.create!(product_id: product.id)
end
def borrowing?(product)
self.transactions.find_by(product_id: product.id, current: true)
end
def borrowed_products
self.transactions.where(current: :true).product
end
end
アプリ/モデル/products.rb
class Product
.
.
.
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
has_many :transactions, dependent: :destroy
has_many :seekers, through: :transactions,
source: :seeker
def borrowed?
self.transactions.find_by(current: true)
end
def borrower
self.transactions.find_by(current: true).seeker
end
end
コードの一部をテストすると、同じタイプの 5 つのテストが失敗し、理由がわかりません。
describe User do
before { @user = User.new(name: "Utilisateur de test",
email: "test@utilisateur.com",
password: "motdepasse",
password_confirmation: "motdepasse") }
subject { @user }
describe "requested product associations" do
let(:lender) { FactoryGirl.create(:user) }
let(:product) { FactoryGirl.create(:product, owner: lender) }
before do
@user.save
@user.request!(product)
end
it { should be_requesting(product) }
its(:requested_products) { should include(product) } # FAIL
describe "when product is borrowed" do
before do
transaction = Transaction.find_by(product: product)
transaction.update_attributes(borrowing_date: 1.day.ago)
transaction.save
end
it { should be_borrowing(product) }
its(:requested_products) { should_not include(product) } # FAIL
its(:borrowed_products) { should include(product) } # FAIL
describe "then returned" do
before do
transaction = Transaction.find_by(product: product)
transaction.update_attributes(return_date: 1.hour.ago)
end
it { should_not be_borrowing(product) }
its(:requested_products) { should_not include(product) } # FAIL
its(:borrowed_products) { should_not include(product) } # FAIL
end
end
end
end
エラーメッセージは次のとおりです。
1) User requested product associations requested_products
Failure/Error: its(:requested_products) { should include(product) }
ActiveRecord::StatementInvalid:
SQLite3::SQLException: ambiguous column name: created_at: SELECT 1 AS one FROM "products" INNER JOIN "transactions" ON "products"."id" = "transactions"."product_id" WHERE "transactions"."seeker_id" = ? AND "products"."id" = 1 ORDER BY created_at DESC LIMIT 1
# ./spec/models/user_spec.rb:174:in `block (3 levels) in <top (required)>'
2) User requested product associations when product has been borrowed borrowed_products
Failure/Error: its(:borrowed_products) { should include(product) }
ActiveRecord::StatementInvalid:
SQLite3::SQLException: ambiguous column name: created_at: SELECT 1 AS one FROM "products" INNER JOIN "transactions" ON "products"."id" = "transactions"."product_id" WHERE "transactions"."seeker_id" = ? AND "transactions"."current" = 't' AND "products"."id" = 1 ORDER BY created_at DESC LIMIT 1
# ./spec/models/user_spec.rb:185:in `block (4 levels) in <top (required)>'
3) User requested product associations when product has been borrowed requested_products
Failure/Error: its(:requested_products) { should_not include(product) }
ActiveRecord::StatementInvalid:
SQLite3::SQLException: ambiguous column name: created_at: SELECT 1 AS one FROM "products" INNER JOIN "transactions" ON "products"."id" = "transactions"."product_id" WHERE "transactions"."seeker_id" = ? AND "products"."id" = 1 ORDER BY created_at DESC LIMIT 1
# ./spec/models/user_spec.rb:184:in `block (4 levels) in <top (required)>'
4) User requested product associations when product has been borrowed then returned requested_products
Failure/Error: its(:requested_products) { should_not include(product) }
ActiveRecord::StatementInvalid:
SQLite3::SQLException: ambiguous column name: created_at: SELECT 1 AS one FROM "products" INNER JOIN "transactions" ON "products"."id" = "transactions"."product_id" WHERE "transactions"."seeker_id" = ? AND "products"."id" = 1 ORDER BY created_at DESC LIMIT 1
# ./spec/models/user_spec.rb:195:in `block (5 levels) in <top (required)>'
5) User requested product associations when product has been borrowed then returned borrowed_products
Failure/Error: its(:borrowed_products) { should_not include(product) }
ActiveRecord::StatementInvalid:
SQLite3::SQLException: ambiguous column name: created_at: SELECT 1 AS one FROM "products" INNER JOIN "transactions" ON "products"."id" = "transactions"."product_id" WHERE "transactions"."seeker_id" = ? AND "transactions"."current" = 't' AND "products"."id" = 1 ORDER BY created_at DESC LIMIT 1
# ./spec/models/user_spec.rb:196:in `block (5 levels) in <top (required)>'
しかし、Rails コンソールでいくつかのテストを手動で実行すると、user.borrowed_products
とはuser.requested_products
問題なく動作します。変 ???