我想了解has_one
RoR中的关系.
假设我有两个模型 - Person
并且Cell
:
class Person < ActiveRecord::Base
has_one :cell
end
class Cell < ActiveRecord::Base
belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)
我可以只使用has_one :person
,而不是belongs_to :person
在Cell
模型?
不一样吗?
我有 2 张桌子(2 个型号)
User
-uid
-email
-password
-(other fields)
Profile
-uid
-name
-age
-phone
-(other fields)
Run Code Online (Sandbox Code Playgroud)
他们有1-1的关系,我实现的关系如下:
class User extends Model
{
public function initialize()
{
$this->hasOne('uid', 'Profile', 'uid');
}
}
class Profile extends Model
{
public function initialize()
{
$this->hasOne('uid', 'User', 'uid');
}
}
Run Code Online (Sandbox Code Playgroud)
这个实现对吗?我可以用belongsTo替换hasOne吗?谢谢你的帮助!:-)