I have two models: Client and Contract.
Client has 3 attributes: id, email and client_number.
Contract has 3 too: id, ip and client_id.
Client has_many :contracts.
Contract belongs_to :client.
With RoR I get Auto-generated methods like:
Client.find_by_email_and_client_number ‘some_email’, 1234
but I want a Auto-generated method like:
Client.find_by_email_and_client_number_and_ip ‘some_email’, 1234, ‘192.168.200.54’
using not Client’s attributes only. I want to use the ip attribute from Contract model too.
You won’t be able to achieve this with Rails’ in-built magic finder methods. But you can do it with a more direct query, like this:
This is an example of two of Rails’ finding mechanisms: the first is finding directly by a hash, the second is a SQL snippet.
It does seem like what you’re actually looking for here is a relation of some sort, though. If
client_numberis set up as the key for thehas_manyassociation, it might be easier to query this like so: