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