"แก้ปัญหาได้แล้ว ดูคอมเมนด้านล่าง"

ใช้อยู่ 3 table ดังนี้

users(id,username,password)
comments(id,body,user_id)
profiles(id,...,user_id)

model มีความสัมพันธ์ดังนี้

class User < ActiveRecord::Base
has_many :comments
has_one :profile
end

class Comment < ActiveRecord::Base
belongs_to :users
end

class Profile < ActiveRecord::Base
belongs_to :user
end

ตัว comments ที่มีความสัมพันธ์ แบบ has_many ใช้ได้ดีไม่มีปัญหา
พอจะใช้ profile แบบ has_one ผมก็เข้าใจว่าแค่เปลี่ยนการเรียกจาก plural เป็น singular แค่นั้น
แต่ profile ที่มีความสัญพันธ์แบบ one-to-one กลับมีปัญหา
เช่น ใน controller นี้


@user = User.find(7)
@profile = Profile.new(params[:newprofile])
@user.profile << @profile

พอรัน Rails แจ้ง error ว่า

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<

งงครับ? มันผิดพลาดที่ตรงไหน

พอเข้าไปอ่าน Doc ของ ActiveRecord

ก็ยังสับสัน เท่าที่ผมอ่านเข้าใจคือ แทนที่จะ user 1 คน จะมี 1 profile

แต่ใน doc อธิบายเป็นว่า 1 profile จะมี user 1 คน ...ผมไม่เข้าใจครับ

ทำไมมันตรงกันข้ามกลับความเป็นจริง ทำไมไม่เหมือน has_many?

อยากให้พี่ๆช่วยอธิบายทีครับ
ขอบคุณมากครับ

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

GiggsWalk Sat, 17/01/2009 - 09:37

Yippy! แก้ปัญหาได้แล้วครับ
คือ แทนที่เราจะใส่ profile โดยสั่ง @user.profile << @profile เหมือน has_many
ใน has_one เราต้องสั่งเป็น @user.profile = @profile แทน
เปลี่ยน '<<' เป็น '='

Sikachu Sat, 17/01/2009 - 20:35

มาขยายความ เพือ่ให้เข้าใจเพิ่ม :)

เนื่องจากว่า ถ้าคุณเรียก attributes ที่เป็น has_many
สิ่งที่มัน return กลับมา จะเป็น array ของ objects ครับ

เช่น

@user.comments # =>  [#<Comment:0x35a5188>,#<Comment:0x35a2474>]
@user.comments.class # => Array

แต่ถ้าเป็น attribute ที่เป็น has_one สิ่งที่ return มาจะเป็น object หรือ nil ครับ (ถ้าไม่ได้ associate กับอะไรเลย)

@user.profile # => #<Profile:0x485ab32>
@user.profile.class # => Profile

เพราะฉะนั้น เหตุผลที่คุณสามารถใช้ << ได้ ก็เพราะว่าจริงๆ แล้วมันไปเรียก Array.<< นั่นเองครับ หรือว่าหมายถึงการเพิ่ม object เข้าใน array นั่นเองครับ

ส่วนของ has_one ที่คุณต้องใช้ = เพราะว่ามันเป็นการกำหนดค่าๆ เดียวครับ เหมือนการกำหนดค่าตัวแปรทั่วๆ ไปนั่นแลครับ :D

บล็อกของผม: http://sikachu.com

ขอบคุณมากครับ คุณ Sikachu
ที่แท้มันก็เรื่องปกติๆนี่เอง ที่งงคงเพราะ เจ้า << นี่แหล่ะครับ ที่เข้าใจผิดไปว่าใช้เชื่อมความสัมพันธ์
เลยพยายามยัดความสัมพันธ์ด้วย << อยู่อย่างนั้น เสียเวลาตั้งนานกับเรื่องนี้ ไม่ไหวเลย 555 ^^'