0

rails 3.2.2 mysql2

I have the following relationships,

class TalkingCase < ActiveRecord::Base

      belongs_to :medical_case

end

class MedicalCase < ActiveRecord::Base

     has_many :talking_cases
end

in the console:

a=TalkingCase.first

a.medical_case

sometimes it return 0 and sometimes it work fine.

and I can use MedicalCase.find(xx) to get the medical_case object.

Do anyone meet this question?

The following is the console ouput:

Loading development environment (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

(this is stranger,I need the medical_case object)

tables in the schema is following:

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
javy_liu
  • 1
  • 2

1 Answers1

1

Please specify the type of the association from the MedicalCase model too

class MedicalCase < ActiveRecord::Base
  has_one :talking_case
end
deefour
  • 34,974
  • 7
  • 97
  • 90
  • The foreign key "medical_case_id" is in table talking_cases,if i use has_one association,I need to add the column "talking_case_id" to table medical_cases,this is not I want! thank you! – javy_liu Jul 12 '12 at 04:42
  • @user1302658 , You will need to have a foriegn key also you need to tell rails about type of associations between them. – Pritesh Jain Jul 12 '12 at 04:51
  • In the object MedicalCase i have add "has_many :talking_cases",and the foreign key "medical_case_id" in the table talking_cases. It's the has_many and belongs_to association.But I try it in the console,when i use "TalkingCase.first.medical_case" still return 1. – javy_liu Jul 12 '12 at 05:01