I have a table with data I pull from an external source. It has three different columns that are hierarchical in nature and reference other tables. The foreign keys are NOT constrained however, so data in those fields is not necessarily valid.
I'm trying to write something generic that will print out any records with values that don't reference an existing row in the parent table for a given date.
I've basically got something like this:
klass.where(:date => date).each do |rec|
next if rec.send(parent)
# do stuff with rec
end
Where klass
is the model for the table and parent
is a symbol of a declared 'belongs_to' association.
This method works, however, there may be tens of thousands of records for the day, but unique values on the key are fewer than 100. The repeated lookups into the parent table are unpleasantly time consuming. What I'd like to do is stash all the keys I've already looked up and only perform the lookup on new keys.
Towards this end, I'd like to be able to retrieve the field name that has the foreign key reference at runtime. Ideally this would work regardless of whether or not it's using the default naming convention.