Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

find_by_id isn't working

#5216

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>'

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

State: stale
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Diego Plentz
    Diego Plentz
    • Tag changed from arel rails3 to arel rails3, arel, rails3.0rc

    July 27th, 2010 @ 05:47 PM

  2. rorra
    rorra

    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 >

    July 27th, 2010 @ 08:11 PM

  3. rorra
    rorra
    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 >
    

    July 27th, 2010 @ 08:13 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • Importance changed from to Low

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

    August 3rd, 2010 @ 06:29 AM

  5. Diego Plentz
    Diego Plentz

    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?

    August 11th, 2010 @ 02:26 PM

  6. Santiago Pastorino
    Santiago Pastorino

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

    August 13th, 2010 @ 02:27 AM

  7. Diego Plentz
    Diego Plentz

    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".

    August 13th, 2010 @ 02:32 AM

  8. Diego Plentz
    Diego Plentz

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

    August 13th, 2010 @ 03:04 AM

  9. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to incomplete

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

    August 13th, 2010 @ 09:54 PM

  10. Kane
    Kane

    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
    

    August 14th, 2010 @ 11:20 PM

  11. Santiago Pastorino
    Santiago Pastorino
    • Assigned user set to José Valim

    August 15th, 2010 @ 12:14 AM

  12. José Valim
    José Valim

    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.

    August 15th, 2010 @ 01:26 PM

  13. Kane
    Kane

    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.

    August 15th, 2010 @ 04:15 PM

  14. José Valim
    José Valim
    • State changed from incomplete to stale

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

    October 11th, 2010 @ 11:19 PM