0

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
3
  • 2
    What exactly is your question? Commented Oct 14, 2013 at 10:21
  • I want to know the difference between two definitions. Commented Oct 14, 2013 at 10:22
  • Please help us help you; edit your question to contain only the relevant code, the code you are actually running that "doesn't work", concrete explanation of how it doesn't work, samples of actual output you get and the output you expected. Commented Oct 14, 2013 at 18:57

2 Answers 2

2

I am not entirely sure what you're asking, what behavior you're seeing, or what behavior you expected. That said, this is what's happening with the code you've pasted (case 2?):

trade = Trade.first
trade.isbn

This returns the Book instance referenced by Trade#isbn.

trade.isbn.title

This is equivalent to

book = trade.isbn
book.title

which returns the title of the Book instance referenced by Trades#isbn. Is this not what you expected?

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for answering my question. I think case1 and case2 is same. I want to access book title in case 1
I am not sure what you mean by "case 1", sorry. You might probably be asking for trade.book.title?
yes I want to know why trade.book.title doesn't work on case 1
0

So Your question is what is difference between symbol (:isbn) and string ("isbn")? In shot symbols are considered Rubys immutable strings You can read more here:

http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/

In general convention is to use symbols as keys inside You options hashes that You pass to methods, though some libs/gems etc support both. But in particular case of Yours it looks like that this value is being typecasted to string, so everything that is passed as option to foreign_key will converted to string using to_s.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.