加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:ActiveRecord :: HasManyThroughSource

发布时间:2020-12-16 19:35:41 所属栏目:百科 来源:网络整理
导读:我有以下代码(有点简化… create_table :signatures do |t| t.integer :signer_id t.integer :card_id t.timestampsend 模型看起来像… class Signature ActiveRecord::Base belongs_to :card belongs_to :userendclass Card ActiveRecord::Base has_many :s
我有以下代码(有点简化…
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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读