Coder Social home page Coder Social logo

bookstore's People

Contributors

sabondano avatar

Watchers

 avatar

bookstore's Issues

Your views regarding calling author_name in book

Hi,
I was working with developing the exact bookstore application with same requirement for terakeet interview. I bumped in to your code after submission and saw you were calling

def author_name
    "#{author.first_name} #{author.last_name}"
end

in the Book class itself.

I wanted to ask for your views of why should book class be responsible for handling author's name ? Doesn't it violate the OOP concepts ? Should not author class handle it OR book class should delegate it to the author class ? Both do the same number of queries.

class Book < ApplicationRecord
  belongs_to :author

  def author_name
    get_author_name(self.author_id)
  end
  def author_name2
    "#{author.last_name} + #{author.first_name}"
  end

  private

  #minimizing class dependecies by providing private methods as external interfaces
  def get_author_name(author_id)
    Author.get_author_name_from_id(author_id)
  end

end

class Author < ApplicationRecord
    has_many :books

    #class methods which provides a gate-way for other classes to communicate through interfaces, thus reducing coupling.   

    def self.get_author_name_from_id(id)
        author = self.find_by_id(id)
        author == nil ? "Author Record Not Found" : "#{author.first_name.titleize} #{author.last_name.titleize}" 
    end

end

compare

Both would do the same number of queries. Just wanted to know your views on this :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.