If you are one of the Ruby On Rails developers , and  trying to make a complex has_many relation , you have one option .. the :finder_sql and :count_sql options.

these options allows you to write a custom query for your relation maintaining the remaining association options.

So if you were quering using the instance variables of the object like this ,

:finder_sql => “select * from attendance_items a where a.employee_id = #{id}”

This will give you wrong results , even more if your query was like this

:finder_sql => “select * from attendance_items a, where a.email = #{email}”

where email is an attribute in Employee… you will get a syntax error !!!

Cause : writing variables in the query between double quotes , makes the variable to be expanded once it was parsed , so in the first case the returned  id will be the object id in the memory not the active record id and in the second case , it will find that no attribute is name email as the object was not loaded.

Solution : Simply replace the double quotes with single quotes , this cause rails to skip expanding the variables.

:finder_sql => ‘select * from attendance_items a ‘+
‘where a.employee_id = #{self.hr_id}’

Advertisements

Facing problems while configuring anything in linux is a normal thing .. may be its boring and tiring .. but its really interesting when you solve them..

As a linux  starter i face some problems while configuring ruby on rails environment for linux .. Here they are

Note : Sure before any thing you need to get ruby and rails installed

  • sudo apt-get install ruby
  • sudo apt-get install rubygems
  • gem install rails –include-dependencies

1.Problem :Any Gem that lets you choose between mswin32 and ruby ( and of course you choose ruby ) is installed

  • Solution : Simply this happens because these gems require compilation so just give them the tool
    • sudo apt-get install g++ ( Now you are done and can work )

2.  Problem : While Installing A gem it tells you this: Could not create Makefile due to        some necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

  • Solution : This Happens because the ruby1.8-dev required library is not installed so simply
    • sudo apt-get install ruby1.8-dev Done

3. Problem : Always installing rmagick gem is a problem but i think its solution in linux is more simple than windows

  • Solution : 3 Steps ( Download imagicmagick , The Lib Magick library , Then the Gem )

    • sudo apt-get install imagemagick
    • sudo apt-get install libmagick9-dev
    • gem install rmagick

4. Problem : The great Problem of the file ( /tmp/mysql.sock ) which makes you not able to use rake db:migrate or any other access to the mysql databases

  • Solution : This Problem arises as the mysql.sock file location and a name has changed during newer versions to be installed in this silly location ( /var/run/mysqld/mysqld.sock) . Really Boring… So now we have to make a symbolic link and also change the name so do this
    • ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

These Problems were very annoying and tiring for me and i hope it helps