I haven't seen much documentation on this so I thought I would talk a little about what I have learned about javascript scope. So say you want to put all your methods into a main class and have sub classes and methods several layers deep. Everything would be so clean and organized but how the heck do you do that in javascript? Unfortunatly javascript does this very different from say ruby or java.

In javascript if you wanted to do something like google.map.Refresh()
first you need to make your main object like this.
google = {}

then make your sub object with a constructor like this.

google.map = function() {
}

then you can add your class methods to your map object like this.

google.map.prototype.Refresh = function() {
//map refresh code
}

if you decided to make google.foo.map.Refresh() all you do is add this in below your main object.

google.foo = {}
google.foo.map = function() {
}
google.foo.map.prototype.Refresh() = function() {
}

It is important to note that you can only add methods to objects with constructors so google.foo can't have a method but google.foo.maps can have the Map method since it was made with a constructor. I hope this helps clarify things while you organize your javascript.

I always knew I would eventually have to bite the bullet and learn javascript since you can't do everything in rails but it seems that times has come. I have been playing with google maps for a bit and I thought I would share my experience with trying to do so without using javascript directly.

I started out using the bhedana/google_maps rails plugin and while documentation was pretty thin it was great for getting a map up quick and easy however as I started going deeper and playing with more advanced functionality I soon found out that this was not going to give me the control I needed to do what I wanted.

Then I found another plugin yawningman/eschaton rails plugin which had quite a bit more documentation and allot more control. So I redid everything using this plugin and with allot of blood sweat and tears I got quite a bit more functionality out of this plugin however now I'm trying to add more advanced features and it looks like I have reached the end of the line for this plugin as well.

One of my main problems while trying to use rails plugins to get google maps in my project without javascript was that there doesn't seem to be a very big user group so I was pretty much on my own to figure things out and the documentation that was available was not great. I think this is because most people who want a very feature rich google maps application just use javascript which makes sense.

So bottom line if you don't know java script and your looking for a quick easy way to put up a very simple google map the bhedana/google_maps rails plugin was nice and easy. If your looking to set up a google map with what I would consider intermediate functionality I would go with the yawningman/eschaton rails plugin. Again there wasn't much of a community to turn to for answers and while there was allot of documentation it not well documented/organized. If you are going to want allot of fancy features and functionality and you don't know javascript you may as well just bite the bullet and get it over with. You will end up saving allot of time and head aches if you do.

Just so there is no misunderstandings I really appreciate all the hard work these guys put into developing these plugins. I had unreolistic expectations for how they should be used so hopefully this will give others else a little incite. Well back to java script.

I originally started this for myself as a neat way to post my problems and hopefully solutions as I go though the wilderness of teaching myself ruby on rails. Since starting this little endeavor I have been working closely with an iphone developer and an android developer as they work though the difficulties of developing in their respective platforms. I mentioned what I was doing and they liked it so much they wanted to contribute as well so I am widening the scope of this blog to include iphone and android development as well as ruby on rails. So even more posts to come. Shweet...

What I'm trying to do is call a helper method from a partial. I have two different views in two different controllers both calling a partial. This partial calls a helper called my_helper so for the view in controller foo the partial should call the my_helper method in the foo_helper and the view in controller bar should call the my_helper method in the bar_helper and all would be right with the world.

If I have a helper method I want accessible to both controllers I would put it in my application_helper.

Well as it stands that is not the case. (at least not out of the box)
While ruby on rails has this great organization for their helpers out of the box it appears they decided to throw a monkey wrench in that intuitive goodness by adding a line by default in the application controller (helper :all) which makes it so you can access a method in any helper from any controller view. Isn't that what the application_helper is specifically for? I can't for the life of me think of a reason why rails would do this by default. I would guess this has caused more than a few n00bs who aren't entirely familiar with helpers to bang their heads against the wall and scream why isn't this working!!!!

Don't get me wrong I like how flexible rails made the helpers by allowing me to make my own and mix and match helpers however I want. I just don't know why they would make every helper available to every controller by default. Well this turned out to be the push I needed to learn about how helpers work so I guess I can't be too annoyed.

I'm sorry if this turned into a rant and I really hope this helps save some poor n00b some time. If you are uber l33t and you happen to know the logic behind why rails adds helper :all to the application_helper by default post a comment. I am dying to know.

I have a quicky since I already figured it out.

So I was messing with my database when I realized that all the fields that I had given a type of decimal didn't actually give me decimal level precision. Everything was all whole numbers. As it turns out that by default when you do a migration like this


def self.up
create_table :foo do |t|
t.column "bar", :decimal
end
end


There is an inherent precision of 10 and a scale of 0. What this means is that you can only do whole numbers. As it turns out precision stands for the total number of numerical charactors that field will store total and the scale is the number of numberical charecters the field will store after the decimal point. So for example a decimal with a precision of 8 and a scale of 2 will store numbers like this 123456.78 or 123.46(rounded up) This also means that the precision must always be greater than or equal to the scale.

Here is an example of how to set the precision and scale of a decimal type field manually.


def self.up
create_table :foo do |t|
t.column "bar", :decimal, :precision => 10, :scale => 6
end
end


Hope this helps.
Best Luck

So what I am trying to do is run build on an object when it is first created (ex. User.new) then set some default values. First I was doing this in the controller:


u = User.new
u.address.build(zip => foo, :state => bar)


BUT

As I understand it though his is the type of thing that should go in the model. I thought I had a solution for this with:


def after_initialize
address.build(:zip => foo, :state => bar)
end


But after a while I started to notice problems. As it turns out after_initialize while seeming to only run after initializeing the model object also runs after a find as well. Obviously this caused problems. So I'm back on the hunt for a new solution.

As always I will post a solution if/when I find one.

Ok so I'm a dumb ass.

As it turns out the belongs_to relationship was working just fine in the xml builder. I was receiving the nil?.name so I thought this meant that the relationship didn't work in xml builder but as it turns out home had an id for a nonexistent person and that bad relationship threw the whole xml builder off the tracks. I just misinterpreted the error and blamed the wrong guy. It were bad data in the database who dun it. Hopefully this silly mistake will be helpful for someone else.

I guess my first post in this little adventure was only successful in embarrassing me. I hope the next one will be a little more helpful and interesting. We'll see.

Original Post

Ok so what i'm trying to do is generate xml using the rails xml builder.

say you have a house and that house has several people living in it. I would have a relationship that looks like this:


class Person < ActiveRecord::Base
belongs_to :house 

end

class House < ActiveRecord::Base
has_may :people

end


Getting rails to build out some xml for the has_many relationship is pretty straight forward. for example:


xml.home do
xml.square_feet home.square_feet
home.people.each do |person|
xml.person do
xml.name person.name
xml.age person.name
end
end
end



which should output this xml

<home>
<square_feet>...</square_feet>
<person>
<name>...</name>
<age>...</age>
</person>
</home>


But in the case where you want to build out xml data for a belongs_to relationship i'm at a loss. I would have thought it would be even easier but that doesn't seem to be the case. Here is how I thought it would work.


xml.person do
xml.name person.name
xml.age person.age
xml.home do
xml.square_feet person.home.square_feet
end
end


which should output this xml

<person>
<name>...</name>
<age>...</age>
<home>
<square_feet>...</square_feet>
</home>
</person>


However for some reason person.home.square_feet does not work in the xml builder despite the fact that it works in the console.

I invite anyone with suggestions on how to make this work to comment or post links otherwise I will hopefully be posting a follow up article on how I eventually figured it out.

Resolved Post