Model1 explicitly belongs_to_one Model2 (via the model2
attribute)
Model2 implicitly has_one Model1
name : " foo"
model2 : " bar"
class Model1
def model2
Model2 . first ( name : data [ 'model2' ]
end
end
class Model2
def model1
Model1 . first ( model2 : data [ 'name' ] )
end
end
One to Many / Many to One
An episode explicitly belongs_to_one Show (via show
attribute)
A show implicitly has_many Episodes
name : " rtn-001"
title : " Podcasten im Futur II"
show : " rtn"
class Episode
def show
# Reference by show name
Show . first ( name : 'rtn' )
end
end
name : " rtn"
title : " Retinauten"
author : " Retinauten"
class Show
def episodes
Episode . find ( show : 'rtn' )
end
end
An episode explicitly belongs_to_many hosts (via hosts
array)
A host implicitly has_many episodes
name : " rtc-s01e03"
title : " Würstchen und Sommer"
show : " rtc"
date : " 1.8.2013"
hosts : [chef, marcel]
class Episode
def hosts
data [ 'hosts' ] . map do |host_name |
Host . first ( name : host_name )
end
end
end
name : " chef"
full_name : " chef"
twitter_name : " grischder"
class Host
def episodes
Episode . find_match ( host : data [ 'name' ] )
end
end