I'm using of Ruby on Rails.
I have some questions about definition of foreign key.
I defined some models.
When I access book title from class Trade via ISBN like this.
trade = Trade.first
trade.isbn #=> just get isbn in case 1.
trade.isbn.title #=> get book title in case 2.
Why case 2 doesn't work as expected??
class Trade < ActiveRecord::Base
attr_accessible :cost, :isbn, :shop_id, :volume
# belongs_to :book, foreign_key: "isbn" # case 1
belongs_to :isbn, class_name: :Book, foreign_key: :isbn # case 2
belongs_to :shop
end
class Author < ActiveRecord::Base
attr_accessible :age, :name
has_many :books
has_many :trades, through
end
class Book < ActiveRecord::Base
self.primary_key = :isbn
attr_accessible :author_id, :cost, :isbn, :publish_date, :title
belongs_to :author
end
class Shop < ActiveRecord::Base
attr_accessible :name
has_many :trades
end