【发布时间】:2012-01-27 03:25:21
【问题描述】:
我有以下代码(有点简化...
create_table :signatures do |t|
t.integer :signer_id
t.integer :card_id
t.timestamps
end
模型看起来像......
class Signature < ActiveRecord::Base
belongs_to :card
belongs_to :user
end
class Card < ActiveRecord::Base
has_many :signatures
has_many :signers, :through => :signatures, :foreign_key => "card_id"
end
class User < ActiveRecord::Base
has_many :sent_cards, :class_name => "Card", :foreign_key => "sender_id"
has_many :received_cards, :class_name => "Card", :foreign_key => "recipient_id"
has_many :signatures
has_many :signed_cards, :through => :signatures, :foreign_key => "signer_id"
end
我在使用 rails 控制台时看到以下错误...
ruby-1.9.2-p0 > u15.signed_cards
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :signed_card or :signed_cards in model Signature. Try 'has_many :signed_cards, :through => :signatures, :source => <name>'. Is it one of :card or :user?
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.1.0/lib/active_record/reflection.rb:517:in `check_validity!'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:27:in `initialize'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:24:in `initialize'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.1.0/lib/active_record/associations.rb:164:in `new'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.1.0/lib/active_record/associations.rb:164:in `association'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.1.0/lib/active_record/associations/builder/association.rb:41:in `block in define_readers'
from (irb):11
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
from /home/slabounty/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
当我添加 source => :card/:user 时,我得到了同样的结果(我相信在这种情况下应该是 :card)。
有什么想法我在这里做错了吗?
显示部分解决方案,因为我想清理 几样东西。迁移与以前的版本保持一致。我现在 看到在签名中找不到 user_id 的 SQL 错误(见下文)。一世 不想这么说,但大多数时候我一直在输入 :foreign_key 任何我想的地方 他们可能无济于事。
class Signature < ActiveRecord::Base
belongs_to :card
belongs_to :signer, :class_name => "User"
end
class Card < ActiveRecord::Base
# Correct
has_many :signatures
has_many :signers, :through => :signatures, :source => :user
end
class User < ActiveRecord::Base
# Wrong!
has_many :signatures, :foreign_key => "signer_id"
has_many :signed_cards, :through => :signatures, :source => :card
end
出现错误(减去堆栈跟踪)
ruby-1.9.2-p0 > u15.signed_cards
Card Load (0.5ms) SELECT "cards".* FROM "cards" INNER JOIN "signatures" ON "cards"."id" = "signatures"."card_id" WHERE "signatures"."user_id" = 15 ORDER BY cards.created_at DESC
SQLite3::SQLException: no such column: signatures.user_id: SELECT "cards".* FROM "cards" INNER JOIN "signatures" ON "cards"."id" = "signatures"."card_id" WHERE "signatures"."user_id" = 15 ORDER BY cards.created_at DESC
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: signatures.user_id: SELECT "cards".* FROM "cards" INNER JOIN "signatures" ON "cards"."id" = "signatures"."card_id" WHERE "signatures"."user_id" = 15 ORDER BY cards.created_at DESC
Card.signers 按预期返回一个空数组。
仍在寻求这方面的帮助。在您不使用相同名称的情况下,我无法以简单、直接的解释方式找到很多内容(即您需要一个 foreign_key 和 source。
【问题讨论】:
标签: ruby-on-rails ruby associations has-many-through