Hi Sergio,

Thanks for your help, the override you suggests works like a charm :-)

Jan


Am Dienstag, 4. Juli 2017 13:57:32 UTC+2 schrieb Sergio Cambra:
>
> Sorry Jan, I understand your problem now 
>
> new_model is needed for search form because rails helpers may need a 
> record to 
> read current value. And new_model is filling some fields for STI models or 
> nested scaffolds, which cause your issue. Not sure if it's easy to fix in 
> activescaffold without breaking other use cases, you could workaround in 
> your 
> erratum controller overriding new_model: 
>
> def new_model 
>   nested? && nested.association.through? ? Erratum.new : super 
> end 
>
> Or simply, return Erratum.new always: 
>
> def new_model 
>   Erratum.new 
> end 
>
> El martes, 4 de julio de 2017 5:02:14 (CEST) Jan Hebler escribió: 
> > Am Montag, 3. Juli 2017 22:20:30 UTC+2 schrieb Sergio Cambra: 
> > > ensure_not_nested must be a method in your app, I don't find it in 
> > > activescaffold code. 
> > 
> > No, Sorry. ensure_not_nested is an Method in active_record. Please see 
> the 
> > Stacktrace here: 
> > 
> > [93, 102] in 
> > 
> /usr/lib64/ruby/gems/2.2.0/gems/activerecord-5.1.2/lib/active_record/associa 
>
> > tions/has_many_through_association.rb 93:         end 
> >     94: 
> >     95:         def build_record(attributes) 
> >     96: #          ensure_not_nested 
> >     97: byebug 
> > =>  98:           record = super(attributes) 
> >     99: 
> >    100:           inverse = source_reflection.inverse_of 
> >    101:           if inverse 
> >    102:             if inverse.collection? 
> > (byebug) where 
> > --> #0 
> > 
> ActiveRecord::Associations::HasManyThroughAssociation.build_record(attribute 
>
> > s#Hash) at 
> > 
> /usr/lib64/ruby/gems/2.2.0/gems/activerecord-5.1.2/lib/active_record/associa 
>
> > tions/has_many_through_association.rb:98 #1 
> > ActiveRecord::Associations::CollectionAssociation.build(attributes#Hash, 
> > &block#NilClass) at 
> > 
> /usr/lib64/ruby/gems/2.2.0/gems/activerecord-5.1.2/lib/active_record/associa 
>
> > tions/collection_association.rb:99 #2  block in 
> > ActiveRecord::AssociationRelation.block in 
> > build(*args#Array, &block#NilClass) at 
> > 
> /usr/lib64/ruby/gems/2.2.0/gems/activerecord-5.1.2/lib/active_record/associa 
>
> > tion_relation.rb:17 #3  ActiveRecord::Relation.scoping at 
> > 
> /usr/lib64/ruby/gems/2.2.0/gems/activerecord-5.1.2/lib/active_record/relatio 
>
> > n.rb:336 #4  ActiveRecord::AssociationRelation.build(*args#Array, 
> > &block#NilClass) at 
> > 
> /usr/lib64/ruby/gems/2.2.0/gems/activerecord-5.1.2/lib/active_record/associa 
>
> > tion_relation.rb:17 #5  ActiveScaffold::Actions::Core.new_model at 
> > 
> /home/jan/src/cmdbfe/vendor/cache/active_scaffold-03dbc4ac900e/lib/active_sc 
>
> > affold/actions/core.rb:266 
> > 
> > There you could see my "fix" to. Everything is working if i comment out 
> the 
> > ensure_not_nested-Method, but i think that this is not the way to go :-) 
> > 
> > You could avoid calling it when action name is index, or move out of 
> > 
> > > new_model method and add it as before_action only in needed actions. 
> > > ActiveScaffold needs to build @record to display search form, so it 
> you 
> > > need search form, new_model must work. 
> > 
> > As my Application should mainly be used for reporting and searching, 
> > avoiding the search form is not really an option. 
> > 
> > Thanks, Jan 
> > 
> > > El lunes, 3 de julio de 2017, 22:17:02 (UTC+2), Jan Hebler escribió: 
> > >> Hi 
> > >> 
> > >> I have an rather complex Data-Model: 
> > >> 
> > >> class Node 
> > >> 
> > >>   has_one :node2server 
> > >>   has_one :server, :through => node2server 
> > >>   has_many :errata, through => :server 
> > >> 
> > >> end 
> > >> class Node2server 
> > >> 
> > >>   belongs_to :node 
> > >>   belongs_to :server 
> > >> 
> > >> end 
> > >> class Server 
> > >> 
> > >>   has_one :node2server 
> > >>   has_one :node 
> > >>   has_and_belongs_to_many :errata 
> > >> 
> > >> end 
> > >> class Erratum 
> > >> 
> > >>   has_and_belongs_to_many :servers 
> > >>   has_many :node2servers 
> > >> 
> > >> end 
> > >> 
> > >> These are two Legacy-Applications (one CMDB and one Patchmanagement). 
> > >> 
> > >> It seems that the new_model Method is always called in the 
> > >> 
> > >> _list_with_header.html.erb: 
> > >>     7:       <% if active_scaffold_config.list.always_show_search %> 
> > >>     8:       <% old_record, @record = @record, new_model %> 
> > >> 
> > >> which in turn calls build_record in 
> > >> active_record/associations/has_many_through_association.rb. 
> Build_record 
> > >> call ensure_not_nested and this throws an 
> > >> HasManyThroughNestedAssociationsAreReadonly-Exception. How could i 
> avoid 
> > >> this? I dont need writable Associations. 
> > >> 
> > >> Thanks, Jan 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails Gem" 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 https://groups.google.com/group/activescaffold.
For more options, visit https://groups.google.com/d/optout.

Reply via email to