On Oct 15, 12:49 pm, RussK <[email protected]> wrote:
> I have a class that represents a simple tree (but usually it's flat).
> It appears that the belongs_to relationship is returning the wrong
> ID.  The class looks like:
>
> class AssetNode < ActiveRecord::Base
>   belongs_to :asset_node # parent
>   has_many :asset_nodes  # children
>   has_and_belongs_to_many :assets  # leaf nodes
>
> The schema looks like:
>
> CREATE TABLE "asset_nodes" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT
> NULL, "node_type" integer, "impact_factor" float DEFAULT 1.0,
> "asset_node_id" integer, "service_id" integer, "created_at" datetime,
> "updated_at" datetime);
>
> and a query of the DB looks like:
>
> sqlite> select * from asset_nodes;
> 833850115|1|88.8||1217222815|2009-10-15 12:52:26|2009-10-15 12:52:26
> 833850116|2|99.9|833850115||2009-10-15 12:52:26|2009-10-15 12:52:26
>
> For a simple test, I fetch the asset node represented by that last
> row, then fetch its parent, by following the asset_node association.
> But it takes two passes to get the right node:
>
>   def test_should_compute_impact_factor
>     asset = assets(:va2)
>     an8 = asset.asset_nodes[0]

There's definitely *something* weird going on. Can you try putting
an8.reload here in the code and see if it changes the results? The
only thing I can think of is the HABTM is somehow leaving incorrect
data behind.

--Matt Jones
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to