Skip to content

Instantly share code, notes, and snippets.

@ybart
Created August 17, 2011 08:34
Show Gist options
  • Save ybart/1151097 to your computer and use it in GitHub Desktop.
Save ybart/1151097 to your computer and use it in GitHub Desktop.
Wrong Relationship
> ActiveRecord::Base.logger = Logger.new(STDOUT)
> Infrastructure::Radio.first
Infrastructure::Radio Load (0.8ms) SELECT "radio_channels".* FROM "radio_channels" LIMIT 1
+----+------------+
| id | ut_id_name |
+----+------------+
| 1 | 245 |
+----+------------+
> counter = 0; Infrastructure::Radio.first.foo.each {counter += 1}; puts counter
Infrastructure::Radio Load (0.4ms) SELECT "radio_channels".* FROM "radio_channels" LIMIT 1
UserTranslation Load (1.0ms) SELECT "user_translations".* FROM "user_translations"
=> 76
> Infrastructure::Radio.first.foo.count
Infrastructure::Radio Load (0.4ms) SELECT "radio_channels".* FROM "radio_channels" LIMIT 1
UserTranslation Load (0.8ms) SELECT "user_translations".* FROM "user_translations"
SQL (0.6ms) SELECT COUNT(*) FROM "user_translations" WHERE ("user_translations".key = 245)
=> 1
# app/models/infrastructure/radio.rb
module Infrastructure
class Radio < ActiveRecord::Base
set_table_name 'radio_channels'
has_many :foo, primary_key: :ut_id_name, foreign_key: :key, class_name: "UserTranslation"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment