0

导轨 3.2.2 mysql2

我有以下关系,

class TalkingCase < ActiveRecord::Base

      belongs_to :medical_case

end

class MedicalCase < ActiveRecord::Base

     has_many :talking_cases
end

在控制台中:

a=TalkingCase.first

a.medical_case

有时它返回 0,有时它工作正常。

我可以使用 MedicalCase.find(xx) 来获取 medical_case 对象。

有人遇到这个问题吗?

以下是控制台输出:

加载开发环境(Rails 3.2.2)

[1] pry(main)> a=TalkingCase.first

  TalkingCase Load (0.4ms)  SELECT `talking_cases`.* FROM `talking_cases` LIMIT 1
=> #<TalkingCase id: 15, user_id: 231, talking_id: 7, nickname: "史丽", medical_case_id: 42, medical_case_name: "糖尿病肾病之一", created_at: "2012-06-21 03:38:36", updated_at: "2012-06-21 03:38:36">(this is ok)

[2] pry(main)> a.medical_case

  MedicalCase Load (0.5ms)  SELECT `medical_cases`.* FROM `medical_cases` WHERE `medical_cases`.`id` = 42 ORDER BY id desc LIMIT 1

=> 1

(这是陌生人,我需要 medical_case 对象)

架构中的表如下:

create_table "talking_cases", :force => true do |t|
    t.integer  "user_id"
    t.integer  "talking_id"
    t.string   "nickname"
    t.integer  "medical_case_id"
    t.string   "medical_case_name"
    t.datetime "created_at",        :null => false
    t.datetime "updated_at",        :null => false
 end

 create_table "medical_cases", :force => true do |t| 
  t.string   "title",                                      :null => false
  t.string   "bianhao",                                    :null => false
  t.integer  "age"
  t.string   "gender",         :limit => 1          
  t.integer  "user_id",                                    :null => false
  t.integer  "is_shared",      :limit => 1
  t.integer  "is_elite",       :limit => 1
  t.integer  "is_recommend",   :limit => 1
  t.string   "share_reason"                                           
  t.string   "other_cate_des"                                         
  t.string   "keywords"
  t.integer  "comments_count",              :default => 0
  t.integer  "click_count",                 :default => 0
  t.integer  "tap",                         :default => 0
  t.datetime "created_at",                                 :null => false
  t.datetime "updated_at",                                 :null => false
  t.integer  "fans_count"
end
4

1 回答 1

1

MedicalCase也从模型中指定关联的类型

class MedicalCase < ActiveRecord::Base
  has_one :talking_case
end
于 2012-07-12T04:10:23.823 回答