This project is archived and is in readonly mode.

#6209 ✓stale
Jake Dempsey

belongs_to returns nil when foreign key column doesnt exists: should at least warn

Reported by Jake Dempsey | December 22nd, 2010 @ 05:15 PM

class SomeTable < ActiveRecord::Base
belongs_to :moo end

If you say SomeTable.first.moo you will get nil. SomeTable does not have a column called moo_id.

Shouldn't this at least throw a warning that the column moo_id does not exist? Maybe even throw some type of exception?

I also see that no sql is actually executed.. Does belongs to have a type of short circuit that first checks the attribute hash and returns nil if the attributes[:fk] returns nil? If that is the case, should it first check that the key is actually in the attributes hash?

Comments and changes to this ticket

  • Alexander Zubkov

    Alexander Zubkov December 22nd, 2010 @ 09:03 PM

    I believe Rails should not handle any case when a programmer makes an error and warn about it. Handling such cases yields additional code, growing in size and losing performance.

    About the fact SQL is not executed at all, it is up to Rails developers to answer.

  • Jake Dempsey

    Jake Dempsey December 22nd, 2010 @ 09:36 PM

    While I can appreciate your comment and agree that overly defensive programming is usually not a good idea and can lead to bloat, I found it strange that I did not get any feedback as to why I was getting nil and no sql was being executed.

    I stumbled on this today because a dev was inquring about a relationship on a model we have. It had a belongs_to :obj relationship but no corresponding col. After digging, it was left over from some refactoring that was done.

    Wanted to at least capture it here for the Rails devs to decide on its fate.

  • rails

    rails March 29th, 2011 @ 01:00 AM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails March 29th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

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>

People watching this ticket

Tags

Pages