代码之家  ›  专栏  ›  技术社区  ›  chris

保存数据时AssociationTypeMismatch问题

  •  2
  • chris  · 技术社区  · 16 年前

    在前面的一个问题中,我被难住了。。但是堆栈溢出提供了解决方案

    我的两个模型

    class Team < ActiveRecord::Base
      has_many :fixtures, :finder_sql => 'SELECT * FROM fixtures where (home_team = #{id} or away_team = #{id})'
      has_many :home_fixtures, :class_name => "Fixtures", :foreign_key => :home_team
      has_many :away_fixtures, :class_name => "Fixtures", :foreign_key => :away_team
      has_many :teamalias
    end
    
    class Fixture < ActiveRecord::Base
      belongs_to :league
      belongs_to :selection
      has_many :selection
    
      named_scope :for_team_id, lambda{|team_id| {:conditions => ['(home_team = ? or away_team = ?)', team_id, team_id]} }
      belongs_to :home_team, :class_name => "Team", :foreign_key => :home_team
      belongs_to :away_team, :class_name => "Team", :foreign_key => :away_team
    
    
      def fix_list
        [home_team.title, "Draw", away_team.title]
      end
    end
    

    Multi level associations in rails

    但是我又被难倒了——我正试图根据上面第一个答案中的最后一个解决方案来保存一个fixture,我得到了一个类型不匹配。

    预期团队(#38391330),获得字符串(#1242130)

    不知道该怎么办,请帮忙。

    这是迁移

    自我介绍 t、 整数:主队 t、 整型:客队 t、 日期时间:什么时候 t、 整数:联盟id

      t.timestamps
    end
    

    结束

    def自动关闭 升降台:固定装置 结束

    创建表格:团队做| t、 字符串:标题

    结束
    

    def自动关闭 投递表格:团队 结束 结束

    类AddResultToFixture<ActiveRecord::迁移 自我介绍 添加列:装置,:结果,:整数 结束

    删除列:装置,:结果 结束

    1 回复  |  直到 9 年前
        1
  •  1
  •   ErsatzRyan    16 年前

    你的表格可能有 fixture[home_team] team.id

    所以当你这么做的时候

    @fixture = Fixture.new(params[:fixture])
    @fixture.save
    

    你在打电话吗 home_team= team.id team.id是字符串,但home\u team应该是团队对象