On Jun 15, 2011, at 9:09 AM, Raklet wrote:

> I have three models.  I have provided their relevant attributes and
> associations for this discussion:
> 
> asset_category_type
>    fields do
>           name :string
>    end
> 
>    has_many :asset_categories
> 
> asset_category
>    fields do
>           name :string #example values: location, equipment, part
>    end
> 
>    belongs_to :asset_category_type
> 
> asset
>    fields do
>           name :string #example values: shop (is a location), tractor
> (is an equipment), wrench (is a tool)
>    end
> 
>    belongs_to :asset_category  # this generates a drop down just
> fine.  Asset should have a category defined
> 
> 
>    has_many :children, :class_name => "Asset", :foreign_key =>
> "parent_id"
>    belongs_to :parent, :class_name => "Asset"  #this is ok too.
> Asset can belong to another asset.
> 
> 
>    has_many :locations, :class_name => "Asset", :foreign_key =>
> "location_id"
>    belongs_to :location, :class_name => "Asset"  #this is the trouble
> spot.  Asset should have a location (another asset) defined, but I
> only want to display assets that are of asset_category_type =
> 'location'

Apologies for the late reply, but this smells distinctly like you've 
over-normalized your data model. If Assets have different behavior and 
associations based on the value in a field, you're really just re-implementing 
STI in a messy way. You may want to think hard about whether or not Location 
should be a real subclass of Asset - doing so will clean up your code and make 
it far more understandable.

For instance, you can then write:

class Asset ...

  belongs_to :location

end

and Rails will automatically make sure you don't accidentally associate, say, a 
tractor to another asset's location field. Hobo will also help you by 
automatically generating a dropdown with only the correct objects in it.

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" 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/hobousers?hl=en.

Reply via email to