Hello All

This is my first time on ruby-forum so pardon if this is not posted
correctly.

I am having a very similar issue regarding has_many through has_many. A
logged user can create a work order and associated fields in models name
alias and sub_tasks. I am using active_admin and an admin user can view
and edit the above mentioned models, but when admin tries to create a
sub_task I can get the following error:

Cannot modify association 'WorkOrder#sub_tasks' because the source
reflection class 'SubTask' is associated to 'NameAlias' via :has_many.

the models:

class work_order < ActiveRecord::Base
has_many :name_aliases
has_many :sub_tasks, :through => :name_aliases
end

class NameAlias < ActiveRecord::Base
belongs_to :work_order
has_many :sub_tasks
end

class SubTask < ActiveRecord::Base
belongs_to :name_alias
belongs_to :work_order
end

A pervious post mentioned a solution using an after_save method before
declaring the association... but I didn't see sample code? since i am
new to rails it would be helpful to see or example how this can be
resolved. Thanks for your help!

Best,
Fritz

--
Posted via http://www.ruby-forum.com/.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Jan 14, 2012 at 9:35 am

    On 14 January 2012 04:12, Fritz Rodriguez wrote:
    Hello All

    This is my first time on ruby-forum so pardon if this is not posted
    correctly.

    I am having a very similar issue regarding has_many through has_many.  A
    logged user can create a work order and associated fields in models name
    alias and sub_tasks.  I am using active_admin and an admin user can view
    and edit the above mentioned models, but when admin tries to create a
    sub_task I can get the following error:

    Cannot modify association 'WorkOrder#sub_tasks' because the source
    reflection class 'SubTask' is associated to 'NameAlias' via :has_many.

    the models:

    class work_order < ActiveRecord::Base
    has_many :name_aliases
    has_many :sub_tasks, :through => :name_aliases
    end

    class NameAlias < ActiveRecord::Base
    belongs_to :work_order
    has_many :sub_tasks
    end

    class SubTask < ActiveRecord::Base
    belongs_to :name_alias
    belongs_to :work_order
    This should be belongs_to :through name_alias. It must mirror the
    has_many sub_tasks :through in work_order

    You should really have started a new thread for this as it is nothing
    to do with the original post.

    Colin

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Fritz Rodriguez at Jan 14, 2012 at 4:59 pm

    Colin Law wrote in post #1040834:
    On 14 January 2012 04:12, Fritz Rodriguez wrote:

    class NameAlias < ActiveRecord::Base
    belongs_to :work_order
    has_many :sub_tasks
    end

    class SubTask < ActiveRecord::Base
    belongs_to :name_alias
    belongs_to :work_order
    This should be belongs_to :through name_alias. It must mirror the
    has_many sub_tasks :through in work_order

    You should really have started a new thread for this as it is nothing
    to do with the original post.

    Colin
    Colin, Thanks for your reply and my apologies regarding proper placement
    of the is post.

    I believe you said the SubTask model should mirror through in
    work_order?

    class SubTask < ActiveRecord::Base
    attr_accessible :name_alias_id, :work_order_id, :status, :title

    belongs_to :name_alias
    belongs_to :work_order, :through => :name_alias

    accepts_nested_attributes_for :name_alias
    accepts_nested_attributes_for :work_order

    This produced a "Unknown key: through" error? Not sure I place the
    association in the correct model, I have attached a file for reference.
    I appreciate your help!

    Attachments:
    http://www.ruby-forum.com/attachment/6910/has_many_through.txt


    --
    Posted via http://www.ruby-forum.com/.

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Jan 14, 2012 at 5:12 pm

    On 14 January 2012 16:59, Fritz Rodriguez wrote:
    Colin Law wrote in post #1040834:
    On 14 January 2012 04:12, Fritz Rodriguez wrote:

    class NameAlias < ActiveRecord::Base
    belongs_to :work_order
    has_many :sub_tasks
    end

    class SubTask < ActiveRecord::Base
    belongs_to :name_alias
    belongs_to :work_order
    This should be belongs_to :through name_alias.  It must mirror the
    has_many sub_tasks :through in work_order

    You should really have started a new thread for this as it is nothing
    to do with the original post.

    Colin
    Colin, Thanks for your reply and my apologies regarding proper placement
    of the is post.

    I believe you said the SubTask model should mirror through in
    work_order?

    class SubTask < ActiveRecord::Base
    attr_accessible :name_alias_id, :work_order_id, :status, :title

    belongs_to :name_alias
    belongs_to :work_order, :through => :name_alias

    accepts_nested_attributes_for :name_alias
    accepts_nested_attributes_for :work_order

    This produced a "Unknown key: through" error? Not sure I place the
    association in the correct model, I have attached a file for reference.
    Sorry, I was talking drivel. belongs_to :through is not supported.
    You should just have belongs_to :name_alias.

    Colin

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Jan 14, 2012 at 5:20 pm

    On 14 January 2012 16:59, Fritz Rodriguez wrote:
    Colin Law wrote in post #1040834:
    On 14 January 2012 04:12, Fritz Rodriguez wrote:

    class NameAlias < ActiveRecord::Base
    belongs_to :work_order
    has_many :sub_tasks
    end

    class SubTask < ActiveRecord::Base
    belongs_to :name_alias
    belongs_to :work_order
    This should be belongs_to :through name_alias.  It must mirror the
    has_many sub_tasks :through in work_order

    You should really have started a new thread for this as it is nothing
    to do with the original post.

    Colin
    Colin, Thanks for your reply and my apologies regarding proper placement
    of the is post.

    I believe you said the SubTask model should mirror through in
    work_order?

    class SubTask < ActiveRecord::Base
    attr_accessible :name_alias_id, :work_order_id, :status, :title

    belongs_to :name_alias
    belongs_to :work_order, :through => :name_alias

    accepts_nested_attributes_for :name_alias
    accepts_nested_attributes_for :work_order

    This produced a "Unknown key: through" error? Not sure I place the
    association in the correct model, I have attached a file for reference.
    I appreciate your help!

    Attachments:
    http://www.ruby-forum.com/attachment/6910/has_many_through.txt
    By the way, in the attachment you have multiple default scopes for
    WorkOrder. I think only one is allowed. You can order by multiple
    fields in one scope of course if that is what you want. Be careful
    with specifying the order in default scope though, it is not possible
    to override this (or at least it did not used to be possible) as the
    default scope order is applied *after* any others.

    Colin

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Fritz Rodriguez at Jan 14, 2012 at 5:51 pm

    Colin Law wrote in post #1040893:
    On 14 January 2012 16:59, Fritz Rodriguez wrote:
    belongs_to :work_order
    of the is post.
    accepts_nested_attributes_for :name_alias
    accepts_nested_attributes_for :work_order

    This produced a "Unknown key: through" error? Not sure I place the
    association in the correct model, I have attached a file for reference.
    I appreciate your help!

    Attachments:
    http://www.ruby-forum.com/attachment/6910/has_many_through.txt
    By the way, in the attachment you have multiple default scopes for
    WorkOrder. I think only one is allowed. You can order by multiple
    fields in one scope of course if that is what you want. Be careful
    with specifying the order in default scope though, it is not possible
    to override this (or at least it did not used to be possible) as the
    default scope order is applied *after* any others.

    Colin
    Thanks, make sense regarding default scope... still getting the error?
    wired, that a logged user can create and edit sub_task, but admin user
    via acitve_admin can edit but no create a sub_task?

    Do you/available for consulting/help with a projects? if so, how does it
    work? per hour, etc..

    Thanks and have a great day

    Best,
    Fritz

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Jan 14, 2012 at 9:41 pm

    On 14 January 2012 17:51, Fritz Rodriguez wrote:
    Colin Law wrote in post #1040893:
    On 14 January 2012 16:59, Fritz Rodriguez wrote:
    belongs_to :work_order
    of the is post.
    accepts_nested_attributes_for :name_alias
    accepts_nested_attributes_for :work_order

    This produced a "Unknown key: through" error? Not sure I place the
    association in the correct model, I have attached a file for reference.
    I appreciate your help!

    Attachments:
    http://www.ruby-forum.com/attachment/6910/has_many_through.txt
    By the way, in the attachment you have multiple default scopes for
    WorkOrder.  I think only one is allowed.  You can order by multiple
    fields in one scope of course if that is what you want.  Be careful
    with specifying the order in default scope though, it is not possible
    to override this (or at least it did not used to be possible) as the
    default scope order is applied *after* any others.

    Colin
    Thanks, make sense regarding default scope... still getting the error?
    Is that a question? Did you add the id column to the table and run
    the migration?
    Show us the complete error message and stack trace and the section of
    your code that is causing it.
    wired, that a logged user can create and edit sub_task, but admin user
    via acitve_admin can edit but no create a sub_task?
    It is executing different code or with different data and causing the
    error. Nothing weird about it.
    Do you/available for consulting/help with a projects? if so, how does it
    work? per hour, etc..
    No, I just try to help out in my spare time.

    Colin

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Jan 14, 2012 at 9:58 pm

    On 14 January 2012 21:41, Colin Law wrote:
    On 14 January 2012 17:51, Fritz Rodriguez wrote:
    Colin Law wrote in post #1040893:
    On 14 January 2012 16:59, Fritz Rodriguez wrote:
    belongs_to :work_order
    of the is post.
    accepts_nested_attributes_for :name_alias
    accepts_nested_attributes_for :work_order

    This produced a "Unknown key: through" error? Not sure I place the
    association in the correct model, I have attached a file for reference.
    I appreciate your help!

    Attachments:
    http://www.ruby-forum.com/attachment/6910/has_many_through.txt
    By the way, in the attachment you have multiple default scopes for
    WorkOrder.  I think only one is allowed.  You can order by multiple
    fields in one scope of course if that is what you want.  Be careful
    with specifying the order in default scope though, it is not possible
    to override this (or at least it did not used to be possible) as the
    default scope order is applied *after* any others.

    Colin
    Thanks, make sense regarding default scope... still getting the error?
    Is that a question?  Did you add the id column to the table and run
    the migration?
    What am I talking about? That was someone else in a different thread.
    Ignore the id comment.

    Colin
    Show us the complete error message and stack trace and the section of
    your code that is causing it.
    wired, that a logged user can create and edit sub_task, but admin user
    via acitve_admin can edit but no create a sub_task?
    It is executing different code or with different data and causing the
    error.  Nothing weird about it.
    Do you/available for consulting/help with a projects? if so, how does it
    work? per hour, etc..
    No, I just try to help out in my spare time.

    Colin


    --
    gplus.to/clanlaw

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Fritz Rodriguez at Jan 14, 2012 at 10:21 pm
    Okay, thanks! Here is the full trace with code section, appreciate any
    insight!

    Attachments:
    http://www.ruby-forum.com/attachment/6913/full_trace.txt


    --
    Posted via http://www.ruby-forum.com/.

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Fritz Rodriguez at Jan 15, 2012 at 3:51 am
    Thanks for your help Colin, resolved the issued by changing my model
    associations....

    class NameAlias < ActiveRecord::Base
    has_many :sub_tasks
    has_many :work_orders, :through => :sub_tasks

    class SubTask < ActiveRecord::Base
    belongs_to :name_alias
    belongs_to :work_order

    class WorkOrder < ActiveRecord::Base
    has_many :sub_tasks
    has_many :name_aliases, :through => :sub_tasks

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 14, '12 at 4:12a
activeJan 15, '12 at 3:51a
posts10
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Fritz Rodriguez: 5 posts Colin Law: 5 posts

People

Translate

site design / logo © 2021 Grokbase