如何在这种情况下设置关系?

时间:2011-03-11 07:38:13

标签: ruby-on-rails

现在我已经创建了这样的方法..

class TestMessage < ActiveRecord::Base
  def test
    Test.find(:first,:conditions => ["test_uri = ?",self.sfl_test_uri])
  end
end

class Test < ActiveRecord::Base
  def test_messages
    TestMessage.find(:all,:conditions => ["sfl_test_uri = ?",self.test_uri])
  end
end

我如何将其转化为关系?

1 个答案:

答案 0 :(得分:2)

尝试类似:

class TestMessage < ActiveRecord::Base
  belongs_to :test, :foreign_key => :sfl_test_uri, :primary_key => :test_uri
end

class Test < ActiveRecord::Base
  has_many :test_messages, :foreign_key => :sfl_test_uri, :primary_key => :test_uri
end