Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Model Reflection Overview

We all use ActiveRecord Reflection on the fly while programming, since we declare all the relations in the model and we are considering it as obvious.
i tried to search and see if there is a way to find those reflections and to alter it in runtime.

Model.reflections


i found this collection which contains all the information about the current model reflections.
for example.

  • .macro: this will return the type of the current reflection (:has_many, etc..)

  • .primary_key_name: the reflection primary key

  • .options: the reflection SQL join options

  • etc. etc.



for example, the following code will show me all the reflections for a model named Article:

  1. >> y Article.reflections.values.collect {|e| e.macro.to_s  + " => " + e.primary_key_name.to_s }  
  2. ---   
  3. - has_many => taggee_id  
  4. - belongs_to => layout_id  
  5. - has_many => article_id  
  6. - has_one => subject_id  
  7. - belongs_to => user_id  
  8. - has_many => taggee_id  
  9. => nil  

2 comments:

  bb

November 22, 2007 at 10:44 AM

Hi Elad,
I'm Rami from Israel.
Nice to see some more Israeli RoR programmers.

In which way ware you using the reflections?

Thanks!

  Elad Meidar

November 22, 2007 at 12:07 PM

Hi rami,
i use the reflections every day and in any project, why not?
if you have anymore question, send me a mail


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels