This project is archived and is in readonly mode.

#5216 ✓stale
Diego Plentz

find_by_id isn't working

Reported by Diego Plentz | July 27th, 2010 @ 05:40 PM

Using Rails 3 RC, find_by_id method isn't working.

ruby-1.9.2-rc2 > xpto = Foo.first
=> # ruby-1.9.2-rc2 > xpto.id
=> 65 ruby-1.9.2-rc2 > Foo.find(65)
=> # ruby-1.9.2-rc2 > Foo.find_by_id(65)
NoMethodError: undefined method find_by_id' for #<Class:0x000001088f29c0><br/>

from /Users/plentz/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/base.rb:938:in `method_missing'
from (irb):6
from /Users/plentz/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/railties-3.0.0.rc/lib/rails/commands/console.rb:44:in `start'
from /Users/plentz/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/railties-3.0.0.rc/lib/rails/commands/console.rb:8:in `start'
from /Users/plentz/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/railties-3.0.0.rc/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:9:in `require'
from script/rails:9:in `<main>'

Comments and changes to this ticket

  • Diego Plentz

    Diego Plentz July 27th, 2010 @ 05:47 PM

    • Tag changed from arel rails3 to arel rails3, arel, rails3.0rc
  • rorra

    rorra July 27th, 2010 @ 08:11 PM

    Could you attach some code and some data to reproduce the error? It works fine for me

    ruby-1.9.2-rc2 > xpto = Project.first
    => # ruby-1.9.2-rc2 > xpto.id
    => 1 ruby-1.9.2-rc2 > Project.find(1)
    => # ruby-1.9.2-rc2 > Project.find_by_id(1)
    => # ruby-1.9.2-rc2 >

  • rorra

    rorra July 27th, 2010 @ 08:13 PM

    ruby-1.9.2-rc2 > xpto = Project.first
     => #<Project id: 1, title: nil, due_date: nil, created_at: "2010-07-27 19:08:51", updated_at: "2010-07-27 19:08:51"> 
    ruby-1.9.2-rc2 > xpto.id
     => 1 
    ruby-1.9.2-rc2 > Project.find(1)
     => #<Project id: 1, title: nil, due_date: nil, created_at: "2010-07-27 19:08:51", updated_at: "2010-07-27 19:08:51"> 
    ruby-1.9.2-rc2 > Project.find_by_id(1)
     => #<Project id: 1, title: nil, due_date: nil, created_at: "2010-07-27 19:08:51", updated_at: "2010-07-27 19:08:51"> 
    ruby-1.9.2-rc2 >
    
  • Santiago Pastorino

    Santiago Pastorino August 3rd, 2010 @ 06:29 AM

    • Importance changed from “” to “Low”

    Diego works for me too, can you show us a better example.
    Or just upload your app

  • Diego Plentz

    Diego Plentz August 11th, 2010 @ 02:26 PM

    Sorry for the delay. Just found the problem: my table pk isn't "id", it is "foo_id" and I map it using set_primary_key "foo_id" (and because you defined as just "id", it works for you). The question is, isn't a expected behavior to get a "find_by_id" always?

  • Santiago Pastorino

    Santiago Pastorino August 13th, 2010 @ 02:27 AM

    you want find_by_id to be like a find_by_your_primary_key???.
    find_by_something looks for a column called something

  • Diego Plentz

    Diego Plentz August 13th, 2010 @ 02:32 AM

    Santiago, I found this after a while, but to me, just feel right to use "find_by_id", since even if my primary_key column isn't called "id", the attribute is mapped to just "id".

  • Diego Plentz

    Diego Plentz August 13th, 2010 @ 03:04 AM

    Btw, this really isn't a "bug", its more like a wish.

  • Santiago Pastorino

    Santiago Pastorino August 13th, 2010 @ 09:54 PM

    • State changed from “new” to “incomplete”

    Ok, you can try to make a patch and tests and we can apply it.
    Thanks.

  • Kane

    Kane August 14th, 2010 @ 11:20 PM

    changing find_by_id to a some kind of find_by_primary_key seems awfull to me. I really dont like this change.
    the find_by_something is clearly for db attributes. in your case id is just not an attribute in the database. Finding by primary key is simply just find.

    Aliasing an attribute also does not work with the finder magic. See example below.
    If you set a custom primary key then the attribute id is also only an alias.
    So why the heck should this work?

    class MyModel < ActiveRecord::Base
      # has a column name
      alias_attribute :some_name, :name
    end
    
    MyModel.finy_by_some_name('Egon') # does not work, cause some_name is no db attribute
    
  • Santiago Pastorino

    Santiago Pastorino August 15th, 2010 @ 12:14 AM

    • Assigned user set to “José Valim”
  • José Valim

    José Valim August 15th, 2010 @ 01:26 PM

    I agree that find_by_primary_key is a bad idea, but when you have "foo.bar_id" as primary key, active record automatically aliases it to "foo.id", so one would expect find_by_id to work as well.

  • Kane

    Kane August 15th, 2010 @ 04:15 PM

    if the dynamic finders are changed so that find_by_id works even if the primary_key is foo_id then this change should support aliasing as a whole. it would only be consistent.

  • José Valim

    José Valim October 11th, 2010 @ 11:19 PM

    • State changed from “incomplete” to “stale”

    Marking as stale until we have a consistent patch for discussion.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages