March 2008


Thanks to Charlie and Russ for the quick and easy directions for getting MySQL Preferences Pane to start and stop the MySQL Server in Mac OS X 10.5 Leopard.

I got tired of seeing the deprecation warnings about the old Rails pagination helpers, so I decided to seek out a better approach, but of course I wasn’t going to implement it myself.  With all of the skilled Rails pros out there, I had every intent of leveraging someone’s else’s success.

I succeeded, thanks to Ilya Grigorik and Alex Wolfe.  Ilya wrote “Faster Pagination in Rails” which leverages Alex’ paginating_find plugin to provide limited result sets at the database level.  Ilya’s post includes code he wrote to provide a neat and tidy presentation of the pagination links.

It only took me a few hours to get it working.  It should not have taken this long, but I’m notorious for making things harder than they need to be.  In this case, after I grabbed the code from Ilya’s post and installed the paginating_find plugin for my application, I modified my view to use Ilya’s helper.  This is where it all went wrong.  I was about to do something like this

<%= render :partial => ‘shared/pagination’ %>

but instead did this

<%= windowed_pagination_links(@results) %>

As it turns out neither one of these is correct, but the second one is even more incorrect than the first and led me down a bad path for a couple hours (just what every developer hates).  Since I don’t ever want to repeat that mistake again, and perhaps someone else out there is having this same trouble, I’m writing about it.

What I saw in my functional tests after modifying my view as above (and the associated controller) was

ActionView::TemplateError: undefined method `page’ for #
On line #22 of app/views/sell/list.rhtml

This frustrated me for hours because I couldn’t figure out why it didn’t recognize the page method.  I checked the code, double-checked Ilya’s blog (and many comments) and checked my code to confirm that I was specifying the :page option to the find method.  Sure enough.  Here’s what my controller had

@stuff = Listing.find_by_all_user(session[:user_id],
:page => {:size => 10, :current => params[:page]})

Now if you know about paginating_find, you may notice a problem with the code above.  But since paginating_find was brand new to me, I didn’t notice it.  I’ll explain the problem here shortly.  First let me explain the problem with my view.

What I really needed to have is

<%= render(:partial => ‘shared/paginate’, :locals => {:collection => @stuff}) -%>

So it turns out that I started out on the right path, but left off a critical piece, which is the passing of the controller-returned collection @stuff to the partial so it can do it’s rendering.  If I had even once executed the code using this render(:partial) approach, I’d have been at least on the right path.

Once I got this figured out, I still had problems.  My controller test was still failing with an error like this

ActionView::TemplateError: undefined method `page_count’ for #
On line #2 of app/views/shared/_paginate.rhtml

I returned to Ilya’s blog and read more comments, re-reviewed the entry itself and didn’t see what I was doing wrong.  Then I looked more closely and noticed that all of the references to the use of the paginating_find plugin were using the find() method of any given model.  So I thought perhaps it’s called paginating_find for a reason, that being that it works only with the find() method, not with the Rails dynamic methods find_by_XXX and find_all_by_XXX.  But I was using find_all_by_user (where user is the property of my model that I wanted to find stuff by.

So a quick update to my code fixed the problem and all tests are passing.

@stuff = Stuff.find(:all, :conditions => ['user=?', session[:user_id]],
:page => {:size => results_per_page, :current => params[:page], :first => 1})

Now I’m a happy camper on the Rails.

I need to be able to assert that a response from a Rails controller doesn’t contain certain content, for example when the same action will respond to both GET and POST methods.  In my case I’m writing a register method in a LoginController and I want it to render the same view, but if the registration was successful I do not want the registration form to be displayed again and instead want some text that answers the question, “Ok, I’ve registered, what next?”

Turns out this is easy (I should have known).  I found the answer on this Cheat Sheet.  The assert_select test helper accepts an equality? parameter, so give it false and it ensures that the content does not contain the value specified in the selector parameter.

The Bible is an incredible book. No other book is like it, period. It stands to reason that no other book would or even could be like it, since it has a very unique characteristic, one that no other book has: it is written by God who created the heavens, the earth and the sea and all things that are therein (Neh.9:6 et al).

The Bible treats of nearly every topic known to man, and I never cease to be amazed at just how much it really does teach us on so many topics, even things most people would never imagine it would. Of course for thousands of years people have disagreed about what the scriptures teach. When Jesus was preaching, he often caused divisions among the people.

One such case is recorded in John 7:40-43.

40 Many of the people therefore, when they heard this saying, said, Of a truth this is the Prophet.

41 Others said, This is the Christ. But some said, Shall Christ come out of Galilee?

42 Hath not the scripture said, That Christ cometh of the seed of David, and out of the town of Bethlehem, where David was?

43 So there was a division among the people because of him.

Jesus’ teaching had caused some to say that he is the Christ, while others knew that he came out of Galilee and the scripture taught that the Christ would come out of Bethlehem, the city of David. Clearly he could not have come out of Galilee and out of Bethlehem, right? Wrong. Jesus was born in Bethlehem, his parents having gone there to be taxed (Lk.2:4,11), but they were from Nazareth, a city of Galilee, and returned there with him after he was born.

Why did God have it that Jesus would come both out of Bethlehem and Galilee?  Did he do it to confuse people?  Does it surprise you that Jesus would cause this kind of division?  It shouldn’t.  Listen to the words of Jesus himself (Lk.12:51-53).

51 Suppose ye that I am come to give peace on earth? I tell you, Nay; but rather division:

52 For from henceforth there shall be five in one house divided, three against two, and two against three.

53 The father shall be divided against the son, and the son against the father; the mother against the daughter, and the daughter against the mother; the mother in law against her daughter in law, and the daughter in law against her mother in law.

If you don’t like that, keep in mind that it’s Jesus with whom you have a problem, not me.  God puts things in his word, like the fact that Jesus both came out of Galilee and out of Bethlehem specifically for the purpose of causing division, of dividing sheep from the goats (see Isa.28:9-13)  How then are we to understand the scriptures with these kinds of snares in them? We must rightly divide the word of truth (2Ti.2:15, Isa.28:9-13), something that requires much diligent study.  If we want to know the truth, we must study the word of God in the way that he teaches us to do (see Jn.7:17) and we must search it out as one searches for gold or silver or rubies or anything else of value (Pro.2:1-5).

Searching for gold is not an easy task (especially in our day).   It requires hard work, sweat and tears, joy and pain.  Gold, silver, rubies and any other worldly treasure are of little worth compared to the true riches which are in Christ (Job 28:8; Pr.3:15,8:11,20:15, Eph.3:8, Php.4:19).

I’m working on a project on a remote server using SSH to connect to a ZSH shell. Of course I’m on my Mac so I’m using Terminal (in Leopard). I love Terminal for local use and I expect it to work just as well when remotely connecting to another server, however the delete key was simply not working. I’d hit it (my dad always laughs at how much I hit the delete key) and it would respond by simply making an annoying sound (without deleting a thing). This grew very frustrating very quickly. In fact if I didn’t solve this problem I would have to find another way to connect to the remote server.

Fortunately, this is an easy problem to solve. Under Terminal->Preferences->Settings->Advanced, click the checkbox next to Delete sends Control-H. Problem solved.

I recently resumed work on a Rails project I started a few months ago only to find that some of the functional tests that Rails generated for some scaffolding code were failing. These tests were failing on a fresh checkout from Subversion, so it was obvious that I’d neglected to run the functional tests before at least my last checking a few months prior. That of course was a problem, but now I’ve corrected that by figuring out a better way to ensure that I can always run the tests.

I spent all day yesterday working on functionality other than the one controller that was failing. I saw the failing tests but decided that since they were on controller code that was generated by scaffolding, I would let them go. I actually thought perhaps the tests were generated wrong, but of course that was not the case. Since I’m now using autotest, I decided I’d better figure out why those tests were failing because I just couldn’t bare to see those failures flashing before me all day while I’m working (and I didn’t want my little project to suffer from the Broken Window theory).

I started by looking at the failures, of which there were four (the real controller name has been changed to protect the innocent).

1) Failure:
test_create(SomeControllerTest)
[/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/assertions/response_assertions.rb:26:in `assert_response'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/assertions/response_assertions.rb:18:in `assert_response'
test/functional/some_controller_test.rb:59:in `test_create']:
response to be a <:redirect>, but was <200>

2) Failure:
test_edit(SomeControllerTest) [test/functional/some_controller_test.rb:72]:
is not true.

3) Failure:
test_show(SomeControllerTest) [test/functional/some_controller_test.rb:40]:
is not true.

4) Failure:
test_update(SomeControllerTest)
[/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/assertions/response_assertions.rb:26:in `assert_response'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/assertions/response_assertions.rb:18:in `assert_response'
test/functional/some_controller_test.rb:77:in `test_update']:
response to be a <:redirect>, but was <200>

4 tests, 10 assertions, 4 failures, 0 errors

So I started by looking at the test for Failure 1 to see if there was anything odd going on. I noticed that the test was trying to create the model without any of its properties. So I went to the code and sure enough, if a save was successful it was redirecting and if not it would render the new action. Since no properties of the model were being passed to the controller, no model was being created. A quick modification to the test to pass in the required properties and it was passing.

At first I hard-coded these properties into the test, but then I took at look at the fixtures and decided I should be using those. It was a good thing I did, too, because it turns out the remainder of the tests were failing due to bad fixture data. The first two fixture entries were missing some properties that, after the original creation of the model, were added as required properties. So I added the missing properties and, to my pleasant surprise, the rest of the failing tests passed.

So the moral is: keep your fixtures updated as you modify your model, especially if adding required properties. AND, more importantly, always run your tests and you’ll know right away as soon as you break something (at which time it will be much easier to figure out what caused it).

2 Chronicles 32

1After these things, and the establishment thereof, Sennacherib king of Assyria came, and entered into Judah, and encamped against the fenced cities, and thought to win them for himself.

2And when Hezekiah saw that Sennacherib was come, and that he was purposed to fight against Jerusalem,

3He took counsel with his princes and his mighty men to stop the waters of the fountains which were without the city: and they did help him.

4So there was gathered much people together, who stopped all the fountains, and the brook that ran through the midst of the land, saying, Why should the kings of Assyria come, and find much water?

5Also he strengthened himself, and built up all the wall that was broken, and raised it up to the towers, and another wall without, and repaired Millo in the city of David, and made darts and shields in abundance.

6And he set captains of war over the people, and gathered them together to him in the street of the gate of the city, and spake comfortably to them, saying,

7Be strong and courageous, be not afraid nor dismayed for the king of Assyria, nor for all the multitude that is with him: for there be more with us than with him:

8With him is an arm of flesh; but with us is the LORD our God to help us, and to fight our battles. And the people rested themselves upon the words of Hezekiah king of Judah.

9After this did Sennacherib king of Assyria send his servants to Jerusalem, (but he himself laid siege against Lachish, and all his power with him,) unto Hezekiah king of Judah, and unto all Judah that were at Jerusalem, saying,

10Thus saith Sennacherib king of Assyria, Whereon do ye trust, that ye abide in the siege in Jerusalem?

11Doth not Hezekiah persuade you to give over yourselves to die by famine and by thirst, saying, The LORD our God shall deliver us out of the hand of the king of Assyria?

12Hath not the same Hezekiah taken away his high places and his altars, and commanded Judah and Jerusalem, saying, Ye shall worship before one altar, and burn incense upon it?

13Know ye not what I and my fathers have done unto all the people of other lands? were the gods of the nations of those lands any ways able to deliver their lands out of mine hand?

14Who was there among all the gods of those nations that my fathers utterly destroyed, that could deliver his people out of mine hand, that your God should be able to deliver you out of mine hand?

15Now therefore let not Hezekiah deceive you, nor persuade you on this manner, neither yet believe him: for no god of any nation or kingdom was able to deliver his people out of mine hand, and out of the hand of my fathers: how much less shall your God deliver you out of mine hand?

16And his servants spake yet more against the LORD God, and against his servant Hezekiah.

17He wrote also letters to rail on the LORD God of Israel, and to speak against him, saying, As the gods of the nations of other lands have not delivered their people out of mine hand, so shall not the God of Hezekiah deliver his people out of mine hand.

18Then they cried with a loud voice in the Jews’ speech unto the people of Jerusalem that were on the wall, to affright them, and to trouble them; that they might take the city.

19And they spake against the God of Jerusalem, as against the gods of the people of the earth, which were the work of the hands of man.

20And for this cause Hezekiah the king, and the prophet Isaiah the son of Amoz, prayed and cried to heaven.

21And the LORD sent an angel, which cut off all the mighty men of valour, and the leaders and captains in the camp of the king of Assyria. So he returned with shame of face to his own land. And when he was come into the house of his god, they that came forth of his own bowels slew him there with the sword.

22Thus the LORD saved Hezekiah and the inhabitants of Jerusalem from the hand of Sennacherib the king of Assyria, and from the hand of all other, and guided them on every side.

23And many brought gifts unto the LORD to Jerusalem, and presents to Hezekiah king of Judah: so that he was magnified in the sight of all nations from thenceforth.

24In those days Hezekiah was sick to the death, and prayed unto the LORD: and he spake unto him, and he gave him a sign.

25But Hezekiah rendered not again according to the benefit done unto him; for his heart was lifted up: therefore there was wrath upon him, and upon Judah and Jerusalem.

26Notwithstanding Hezekiah humbled himself for the pride of his heart, both he and the inhabitants of Jerusalem, so that the wrath of the LORD came not upon them in the days of Hezekiah.

27And Hezekiah had exceeding much riches and honour: and he made himself treasuries for silver, and for gold, and for precious stones, and for spices, and for shields, and for all manner of pleasant jewels;

28Storehouses also for the increase of corn, and wine, and oil; and stalls for all manner of beasts, and cotes for flocks.

29Moreover he provided him cities, and possessions of flocks and herds in abundance: for God had given him substance very much.

30This same Hezekiah also stopped the upper watercourse of Gihon, and brought it straight down to the west side of the city of David. And Hezekiah prospered in all his works.

31Howbeit in the business of the ambassadors of the princes of Babylon, who sent unto him to enquire of the wonder that was done in the land, God left him, to try him, that he might know all that was in his heart.

32Now the rest of the acts of Hezekiah, and his goodness, behold, they are written in the vision of Isaiah the prophet, the son of Amoz, and in the book of the kings of Judah and Israel.

33And Hezekiah slept with his fathers, and they buried him in the chiefest of the sepulchres of the sons of David: and all Judah and the inhabitants of Jerusalem did him honour at his death. And Manasseh his son reigned in his stead.

***************************

The first thing Hezekiah did when he learned of the pending attack from the king of Assyria was he took counsel with his princes and mighty men. Hezekiah knew “Where no counsel is, the people fall: but in the multitude of counselors there is safety” (Pr.11:14) and that “Without counsel purposes are disappointed: but in the multitude of counsellors they are established.” (Pr.15:22) The next thing Hezekiah did was strengthen himself and built up the wall of the city and made arms for his people. “A wise man is strong; yea, a man of knowledge increaseth strength. For by wise counsel thou shalt make thy war: and in the multitude of counsellors there is safety.” (Pr.24:5-6)

After setting captains of war over the people and gathering them together, he encouraged them to “Be strong and courageous, be not dismayed for the king of Assyria, nor for all the multitude that is with him: for there be more with us than with him: With him is an arm of flesh; but with us is the LORD our God to help us, and to fight our battles. And the people rested themselves upon the words of Hezekiah king of Judah.” Hezekiah knew what the LORD had said to Jeremiah, “Thus saith the LORD; Cursed be the man that trusteth in man, and maketh flesh his arm, and whose heart departeth from the LORD. For he shall be like the heath in the desert, and shall not see when good cometh; but shall inhabit the parched places in the wilderness, in a salt land and not inhabited. Blessed is the man that trusteth in the LORD, and whose hope the LORD is.” (Jer.15:5-7)

We, in our flesh, oftentimes forget that “we wrestle not against flesh and blood, but against principalities, against powers, against the rulers of the darkness of this world, against spiritual wickedness in high places.” (Eph.6:12) We forget that just because we cannot see the heavenly host round about us doesn’t mean it’s not there (see 2Ki.6:14-17). We have a tendency to look at the odds without taking into consideration that one man with God is more than multitudes of men without God. We need to remember, as Hezekiah, that “When a man’s ways please the LORD, he maketh even his enemies to be at peace with him.” (Pr.16:7)

After this the king of Assyria sent his servants to Jerusalem to gainsay the encouraging words of Hezekiah. Verses 10-19 record their words and tactics to weaken the people whom Hezekiah had strengthened. They spoke against God, making him to be no greater than the gods of any of the other nations that the king of Assyria had destroyed. They painted Hezekiah as a foolish leader who vainly trusted in God to deliver them. “Doth not Hezekiah persuade you to give over yourselves to die by famine and by thirst, saying, The LORD our God shall deliver us out of the hand of the king of Assyria?” (2Ch.32:11) This is a classic, if not the classic, device of the devil — casting doubt upon the word of God. Remember how the devil tricked Eve into partaking of the fruit of the tree of the knowledge of good and evil: “Yea, hath God said…?” Asking questions that cast doubt upon the word of God is easy to do and it’s a powerful device of the devil because it caters to the natural tendency of man to reject the things of the Spirit of God as so much foolishness. (1Co.2:14)

The Assyrian king’s servants also asserted that Hezekiah had removed the high places of God, when it was the high places of idolatry that Hezekiah had removed. (2Ch.32:12 c/w 2Ch.31:1) This is another common device of the devil — to persuade men that all religion is equally valid, that mixing the worship of idols with the worship of God is a good thing, that it is better that worshipping God should be easy rather than hard, etc. Hezekiah had restored the due order of worship and sacrifice to God (which things go hand in hand) which meant that the people must worship before the altar in Jerusalem rather than in all the groves and high places scattered throughout the land (that Hezekiah had destroyed). The point is that while Hezekiah had done well in destroying the high places of idolatry, the servants of the king of Assyria painted a different picture, one in which Hezekiah looked like an intolerant zealot who was destroying God’s places of worship.

Furthermore they reminded the people of how powerful the king of Assyria was and how many nations he had destroyed and how the gods of those nations failed to deliver them. “How much less shall your God deliver you out of mine hand?” (2Ch.32:15) The Assyrian king launched an all out campaign to scare the Lord’s people, even writing “letters to rail on the LORD God of Israel, and to speak against him…” (2Ch.32:17) All this served to frighten the people into submission so they could take the city.

These things sound very familiar. “The thing that hath been, it is that which shall be; and that which is done is that which shall be done: and there is no new thing under the sun.” (Ecc.1:9) Yes, these same things happen today in civil governments of all kinds. It should be no surprise to us, for it is the same principalities and powers at work today that were at work in the days of good king Hezekiah. Let us rest upon his encouraging words as the people did in his day: “Be strong and courageous, be not afraid nor dismayed for the king of Assyria, nor for all the multitude that is with him: for there be more with us than with him. With him is an arm of flesh; but with us is the LORD our God to help us, and to fight our battles.” (2Chr.32:7-8)