Method: Associatable#has_one_through

Defined in:
lib/easy_save/associatable.rb

#has_one_through(name, through_name, source_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/easy_save/associatable.rb', line 83

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] 

    sql = "    SELECT\n      \#{source_options.table_name}.* \n    FROM\n      \#{through_options.table_name} \n    JOIN\n      \#{source_options.table_name} \n    ON\n      \#{through_options.table_name}.\#{source_options.send(:foreign_key)} \n      = \#{source_options.table_name}.\#{source_options.send(:primary_key)} \n    WHERE\n      \#{through_options.table_name}.\#{through_options.send(:primary_key)}\n    SQL\n\n    res = DBConnection.execute(sql)\n    source_options.model_class.new(res.first)\n  end\nend\n"