67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/monorm/associatable.rb', line 67
def has_one_through(name, through_name, source_name)
define_method(name) do
through_options = self.class.assoc_options[through_name]
source_options = through_options.model_class.assoc_options[source_name]
through_options_table = through_options.table_name
through_options_foreign_key = through_options.foreign_key
through_options_primary_key = through_options.primary_key
source_options_table = source_options.table_name
source_options_foreign_key = source_options.foreign_key
source_options_primary_key = source_options.primary_key
search_key = self.send(through_options_foreign_key)
has_one_results = MonoRM::DBConnection.execute(<<-SQL, search_key)
SELECT
#{source_options_table}.*
FROM
#{through_options_table}
JOIN
#{source_options_table}
ON
#{source_options_table}.#{source_options_primary_key} = #{through_options_table}.#{source_options_foreign_key}
WHERE
#{through_options_table}.#{through_options_primary_key} = INTERPOLATOR_MARK
SQL
source_options.model_class.parse_all(has_one_results).first
end
end
|