4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/railz_lite/models/associatable2.rb', line 4
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_table = through_options.table_name
through_pk = through_options.primary_key
through_fk = through_options.foreign_key
source_table = source_options.table_name
source_pk = source_options.primary_key
source_fk = source_options.foreign_key
key_val = self.send(through_fk)
results = DBConnection.execute(<<-SQL, key_val)
SELECT
#{source_table}.*
FROM
#{through_table}
JOIN
#{source_table}
ON
#{through_table}.#{source_fk} = #{source_table}.#{source_pk}
WHERE
#{through_table}.#{through_pk} = ?
SQL
source_options.model_class.parse_all(results).first
end
end
|