0

Which is preferred and why?

Car.find_by(make: chevy)

or

Car.find_by(make_id: chevy.id)
Monti
  • 643
  • 6
  • 18
  • 1
    The both end up using the same exact SQL, so use whichever you prefer. – infused Jun 19 '15 at 18:06
  • @infused Does that mean it would be redundant to create a `make_id` column for `Car`? – Monti Jun 19 '15 at 18:07
  • 2
    No, it would not work without a make_id. The first query `Car.find_by(make: chevy)` simply gets converted to `Car.find_by(make_id: chevy.id)` under the hood. – infused Jun 19 '15 at 18:09
  • 2
    @MarsAtomic How it is duplcate ? – Arup Rakshit Jun 19 '15 at 18:20
  • 1
    @ArupRakshit The linked question asks whether to use find, find_by or where. The accepted answer is virtually identical to infused's comments. I don't think a question has to be a word-for-word facsimile to be considered a duplicate when it covers the exact same material and comes to the same conclusions. – MarsAtomic Jun 19 '15 at 18:24
  • @MarsAtomic No it is not duplicate.. This question has a valid answer on its own.. – Arup Rakshit Jun 19 '15 at 18:26
  • 1
    @nmonti You can either. As you have `make` attribute on the model, so Rails will definer _setter_ and _getter_ for you. Thus your _first version_ will work perfectly as your second version.. – Arup Rakshit Jun 19 '15 at 18:29
  • @ArupRakshit Even if I knew nothing, I can look at that supposedly totally different question and answer this one because the underlying mechanism is the same, but that's me. I don't see how you think "The both end up using the same exact SQL" is different from "Use whichever one you feel suits your needs best," but that's you. People see things differently. – MarsAtomic Jun 19 '15 at 18:39
  • @MarsAtomic It is not the same at all. I've actually used that link before when I didn't know the difference between `find_by` and `where`. It mentions nothing about the underlying SQL, which is what I wanted to know in this question. – Monti Jun 19 '15 at 19:15
  • @nmonti Please re-read the question. The OP actually specifically mentions that the underlying SQL is the same. – MarsAtomic Jun 19 '15 at 20:11

0 Answers0