0

I searched around the SOF and couldn't find a simple answer for my simple question.

I have two classes, Author and Book.

class Author < ActiveRecord::Base def greet puts "Hello, I'm #{name}." end def describe puts "That Author's name is #{name}. puts "This author wrote this book #{book}." #<--- I want to write this or combine it with the line above end end 

In class book, I don't have anything just

class Book < ActiveRecord::Base end

How can I puts the following: That Author's name is ... and This author wrote this ... when I enter Author.first.describe in the Rails console?

4
  • firstly few syntax error in the code, also what are you trying to achieve here.. im not getting. Commented Jan 31, 2017 at 1:15
  • as stated above, its not quite clear what your goal is, do you have any relations defined that connect Author and Book? What is your expected output from the #{book} segment in your string? What have you attempted so far? More information and clarity would go a long way to helping us provide an answer for you Commented Jan 31, 2017 at 1:17
  • @JohnHayes-Reed Each author has a 1 book and it's assigned to him in his table with a book_id which matches the book_id in the books table. Commented Jan 31, 2017 at 1:55
  • I have written an answer based on the information you just commented, hope it can help you or get you on your way. Commented Jan 31, 2017 at 2:14

2 Answers 2

2

based on the information you gave me in the comment your relations would look something like this:

class Author < ActiveRecord::Base belongs_to :book end class Book < ActiveRecord::Base has_one :author end 

The describe method in your Author class could look something like

def describe "That Author's name is #{name}. This author wrote this book #{book.title}." end 

This is based on you stating you have the book_id in your authors table, and I am also assuming you have a title or name field in your books table.

However, it feels more natural to have the Author owning the book, and the book belonging to the Author, so might I suggest changing your data structure a bit to remove the book_id from authors table, and instead place an author_id in book table, and then your models relations would look like this:

class Author < ActiveRecord::Base has_one :book end class Book < ActiveRecord::Base belongs_to :author end 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I think I messed up the relationships, and it's working now!
1
class Author < ActiveRecord::Base has_many :books class Book < ActiveRecord::Base belongs_to :author 

and in the migration author:references

 class CreateServices < ActiveRecord::Migration[5.0] def change create_table :services do |t| t.string :title t.text :description t.text :require t.integer :price t.references :user, foreign_key: true t.timestamps end end end 

this is a project i'm working on, i'm using user but in your case is author.

2 Comments

Thank you, I actually have the tables set up. I just need to output the author's name and the books when I enter for example: Author.first.describe in the console
Thank you, I think I messed up the relationships, and it's working now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.