has_many :through => has_many

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

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 Law wrote in post #1040834:

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

Sorry, I was talking drivel. belongs_to :through is not supported.
You should just have belongs_to :name_alias.

Colin

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

Colin Law wrote in post #1040893:

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

Colin Law wrote in post #1040893:

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

Colin Law wrote in post #1040893:

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