ruby - ActiveRecord and might belongs_to -
i have pair of data types every x
may have many y
, , each y
has @ 1 x
.
in database, i'd visualized as
create table xs ( id integer not null primary key ); create table ys ( id integer not null primary key, x_id integer foreign key references xs (id) -- may null );
using activerecord, it's easy me every x
has_many
y
, how express every y
has @ 1 x
? impression belongs_to
work, i'm not sure how it'll situation when x_id
null
.
you're right. y should have belongs_to :x
.
if x_id
not present, y.x
return nil
.
having belongs_to doesn't mean if value not present, blow up.
Comments
Post a Comment