0

I am a beginner in Ruby. I am providing below a code snippet which I want to know the meaning of the syntax of. I know the semantics, in a sense that I can run the full length code and see what happens. But I am not clear on the language construct itself.

class Product < ActiveRecord::Base
    include ActsAsTree

    self.primary_key = 'id'
    acts_as_tree foreign_key: 'product_id'

Questions follow:

  1. Is ActiveRecord a module provided by ActiveRecord gem?
  2. Is ActiveRecord::Base a class contained in ActiveRecord module?
  3. Is primary_key an attribute of ActiveRecord::Base class?
  4. The last line of the code confuses me the most. acts_as_tree is the name of the gem. Why does it precede in the statement? And what the heck is foreign_key: 'product_id'? Why is there a colon(:) separator in between? What language construct does the statement represent?
3
  • Might I suggest reading the source material and see how many of your questions are answered? Commented May 19, 2015 at 19:53
  • 2
    4) Because it's not just the name of the gem--it's also a class method. It's a map argument to acts_as_tree. I'd strongly consider boning up on some Ruby basics. Commented May 19, 2015 at 19:59
  • 1
    acts_as_tree foreign_key: 'product_id' is equal to act_as_tree(:foreign_key => 'product_id') Commented May 19, 2015 at 20:27

2 Answers 2

1
  1. Yes ActiveRecord is a module provided by ActiveRecord gem

  2. Base is a class contained in ActiveRecord module.

  3. By default activerecord treats id column in a table as primary key. If we have primary key with different name in table then we use self.primary_key to inform activerecord about the primary key. primary_key is a class method. You can check it here http://apidock.com/rails/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods/primary_key. You can also check the source code to better understand how the method is written.

  4. acts_as_tree is a gem name and this gem have acts_as_tree class method defined in it. acts_as_tree foreign_key: 'product_id' line is just calling acts_as_tree method with parameter(foreign_key: 'product_id'). Above line is equivalent to acts_as_tree(foreign_key: 'product_id') or acts_as_tree(:foreign_key => 'product_id'). Parameter to the function is a Hash object. Hash is a class in ruby.

Learn some basic stuffs about ruby before starting to write code. You can start here http://www.tutorialspoint.com/ruby/

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

Comments

1

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb

  1. ActiveRecord is a module.
  2. Base is a class inside the module
  3. primary_key is a class level var (see the self. in front). Active Records internals use that if defined to identify the pk for your table (if not defined I think it's using 'id' by default)
  4. as comments say foreign_key: 'blah' is just a fancy notation for :foreign_key=>'blah. the parenthesis are implied, the fact that it's a hash is also implied

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.