Posts

Showing posts from March 3, 2019

Rails - correct associations for pre-existing models

Image
2 I am trying to create Rails models for some pre-existing database tables. There is 1 MainTable, which has 1 ChildTable. The problem could be the 'foreign keys' are named differently on each table class MainTable < ActiveRecord::Base has_many :child_tables, :class_name => 'ChildTable', :foreign_key => "child_column_name" accepts_nested_attributes_for :child_tables self.primary_key = "main_table_column_name" self.table_name = 'main_table' end class ChildTable < ActiveRecord::Base belongs_to :main_table, :class_name => 'MainTable', foreign_key: "child_column_name", :primary_key => "main_table_column_name" self.table_name = 'child_table' self.primary_key = ...