ruby-on-rails – Rails:ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:找不到源关联

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails:ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:找不到源关联前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码(有点简化…
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,:foreign_key => "recipient_id"

    has_many :signatures
    has_many :signed_cards,: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,: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 => :卡/:用户(应该是:在这种情况下我相信卡).

任何想法我在这里做错什么?

显示部分解决方案,因为我想清理一个
一些事情.迁移与以前的版本保持一致.我现在
看到sql错误(见下文),它在Signature中找不到user_id.一世
讨厌说,但大多数我一直在加入:foreign_key whereever我想
他们可能无济于事.

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,:source => :user

    end

    class User < ActiveRecord::Base
        # Wrong!
        has_many :signatures,:foreign_key => "signer_id"
        has_many :signed_cards,: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和来源).

解决方法

用户应该这样定义:
class User < ActiveRecord::Base

  has_many :sent_cards,:foreign_key => "sender_id"
  has_many :received_cards,:foreign_key => "recipient_id"

  has_many :signatures
  has_many :signed_cards,:source => :card

end

当您的关联名称与以下使用的名称不同时:您必须定义源参数.如果你查看异常消息,它会明确地要求你这样做.

原文链接:https://www.f2er.com/ruby/271441.html

猜你在找的Ruby相关文章