I'm wondering whether the following is possible in Rails. I'm pretty new to Rails, but it just seems like the type of thing I would expect Rails to make possible...
I am wondering whether a DB method (for example, find) can take an object and know automatically to look for its ID value in the appropriate foreign key column without being given the name of that column.
My specific use case is that I would like to make a generic method that checks whether two models are correlated via a connection-table or not.
(In other words, different tables will be linking different models, and thus will use different column names. I want to be able to pass in something [presumably objects of the relevant classes] and Rails will know that the id of @sample_model of class SampleModel should be searched for in the column sample_model_id.)
So the method would be something like:
def connected? (connection_table, model1, model2)
connection_table.class.find_by(model1, model2)
end
…and the method could know automatically that it's looking for model1.id in the model1_id column and the model2.id in the model2_id column.
Is this (or something like it) possible? If not, is there any other way to accomplish my goal (the generic-connection-checker method)?