I have run into this same issue today, but I worked around it on the 
ActiveRecord level instead.

Acts_as_tree, when applied to a model, sets up a belongs_to :parent and a 
has_many 
:children association, supported by the existing database field parent_id. 
Hobo then applies its magic to make these accessible, directly accessible 
the parent_id field through ActiveRecord.

With ancestry however, there is no parent_id field in the database, so when 
I set up the same belongs_to :parent association, and then went through 
Hobo's model and controller to update the parent, I received an 
"ActiveModel::MissingAttributeError: can't write unknown attribute 
`parent_id'" exception.

Turns out Hobo's  "belongs_to_with_accessible" 
(lib/hobo/model/accessible_associations.rb) overwrites (and thereby 
defeats) ancestry's parent= setter, that's why the model ends up attempting 
to write to its (nonexistent) "parent_id" database field.

So I copied ancestry "parent=" setter code into my model, reestablishing 
the intended functionality:
 

>   belongs_to :parent, class_name: "...", foreign_key: "parent_id", 
> accessible: true
>  
>   has_ancestry
>
>   # HACK - re-establish ancestry setter method to make things roll again 
> against Hobo-accessible AR
>   def parent= parent
>     write_attribute(self.ancestry_base_class.ancestry_column, if 
> parent.nil? then nil else parent.child_ancestry end)
>   end
>

With this the view layer happily returned to Hoboland where everything 
works magically as intended without explicit configuration. :)

Enjoy!

PS: Any interest in putting code into Hobo to avoid this conflict? I do not 
have the time to look into the internals of Hobo currently, but given 
specific instructions on where I should put what I could probably churn out 
a pull request.

2011. október 2., vasárnap 5:27:32 UTC+2 időpontban Banjoey a következőt 
írta:
>
> OK, so I got a little further. I used the <select> tag and am 
> searching for menu entries, but am having trouble populating the list. 
> I have ancestry set up properly and migrated, and here's my (relevant) 
> model code: 
>
>   fields do 
>     name      :string, :unique, :name => true 
>     ancestry  :string, :index => true 
>     timestamps 
>   end 
>
>   belongs_to :post 
>   has_ancestry 
>
> Then, I created a merged form in application.dryml which looks like 
> this: 
>
> <extend tag="form" for="MenuEntry"> 
>   <old-form merge multipart> 
>     <field-list: fields="name, post, parent_id"> 
>       <parent-id-view:> 
>       <% @menu_entries = MenuEntry.all %> 
>       <if test="&@menu_entries"> 
>         <select options="&@menu_entries"/> 
>       </if> 
>       <else> 
>           <select disabled><option>No Parents Available</option></ 
> select> 
>         </else> 
>       </parent-id-view:> 
>       </field-list:> 
>   </old-form> 
> </extend> 
>
> Now, when I create the first menu entry, I get the "No Parents 
> Available" entry (disabled), and I can create other entries as well. 
> However, after I create the first, I would expect to have a list of 
> all menu entries in the drop down. If I put a <% puts... %> statement 
> I get all the menu_entries that are saved to the db. However, those 
> menu entries do not show up in the drop down. 
>
> I'm grasping at straws here, but do I need to iterate and get the 
> names of the entries (I have a field set for name in the model as you 
> can see above)? Is something not "translating" correctly since I am 
> not using the <select-one> tag (which won't work since this isn't a 
> belongs_to relationship)? 
>
> Any help would be appreciated very much. I feel like I'm almost 
> there!!! :) 
>
>
>
> On Sep 30, 1:27 am, Banjoey <[email protected]> wrote: 
> > OK, I have installed the Ancestry gem, migrated the db fields, and am 
> > working on my very first recipe to contribute to the cookbook. 
> > However, I've hit a roadblock and need some help... 
> > 
> > I have a model that I'm going to use for a menu system, and the model 
> > is called MenuEntry. It just has a name :string and the 
> > ancestry :string fields, plus an associated belongs_to :post 
> > relationship. I think I'd like to set it up to where you simply pick a 
> > parent menu entry in the new/edit forms. If I understand the ancestry 
> > gem correctly I'll just need to provide the user with a drop down list 
> > of all the available menu_entries and then pass the selected one to 
> > the new method of the controller when creating this one. 
> > 
> > 1) How do I create a drop down of all the menu entries? It looks to me 
> > like <select-one> would be the best way to go, but is it only usable 
> > on models that are set up to have a belongs_to relationship, or can I 
> > override that with something like <select-one 
> > entries="#{MenuEntry.all}">... or something like that? 
> > 
> > 2) Once I get that, can I pass the selected menu_entry to the new 
> > controller method and do what I need with it? I think the code I'd 
> > need in a non-hobo app would be something like this: @menu_entry = 
> > MenuEntry.new(:parent_id => params[:parent_id]) but I was wondering if 
> > I could pass those parameters into hobo_new like this: 
> > hobo_new :parent_id => params[:parent_id] 
> > 
> > I think the :parent_id is set up when you declare the model with 
> > has_ancestry, so I assume that means it is "hidden" to hobo, and all 
> > the information is stored in that string field anyway. Any ideas on 
> > where I can look? 
> > 
> > Thanks, 
> > Joey

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/hobousers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to