-
Notifications
You must be signed in to change notification settings - Fork 330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
undefined method `find_all' when saving an Record #411
Comments
If bug is triggered by Connection.first.save! on rails console, it's a rails bug |
Am 16.03.2015 um 09:45 schrieb Sergio Cambra:
|
Post a backtrace including active_scaffold lines so I can check what is triggering find_all call |
Am 16.03.2015 um 15:28 schrieb Sergio Cambra:
|
You can avoid this error setting :autosave => false on has_one :node, :through => :interfaces association: has_one :node , :through => :interface, :autosave => false It's a rails bug, you can reproduce it without activescaffold setting :inverse_of in through associations: # Connection
has_one :node , :through => :interface, :inverse_of => :connections
# Node
has_many :connections, :through => :interfaces, :inverse_of => :node ActiveScaffold improves autodetection of inverse association, and adds autodetection for through association, so it has the same effect as defining inverse_of on these through associations. When running Connection.first.save it tries to save Connection.first.node too, and defining inverse_of sets a wrong association object for :connections association, because target is not an array. This is association object in node record: target is |
More investigation in this issue:
When association is preloaded, it calls set_inverse_instance, which sets target for inverse association, with "inverse.target = owner". However if inverse association is a has_many association it should set [owner] as target. This doesn't happen when a belongs_to association has inverse_of, because it only sets target for inverse association if it's a has_one association. So target should be set to [owner] or has_one through associations with inverse has_many should skip set_inverse_instance as belongs_to does. First fix would be: ActiveRecord::Associations::CollectionAssociation.class_eval do
def target=(target)
super Array(target)
end
end Second fix: ActiveRecord::Associations::ThroughAssociation.module_eval do
# NOTE - for now, we're only supporting inverse setting back onto has_one associations.
def invertible_for?(record)
inverse = super
inverse && !inverse.collection?
end
end |
I'm adding second fix to activescaffold, so it works while this bug is not fixed in rails. |
…iation is a has_many association, relates #411
Hi
I have this Models:
Node has_many Interfaces
Node has_many Connections through interfaces
Interface has_many Connections
Network has_many Connections
Connection has_many Ips
Connection belongs_to Interface
Connection has_one Node through Interface
in Connection this Statement:
default_scope{ includes(:interface, :node) }
when i try to save an existing(via Seed) Connection, i get this Error:
as my english is not the best, i have set up an Example at: https://github.com/Ladeburger/find_all
In this Sample, the Bug can be triggered by:
rails c
Connection.first.save!
The text was updated successfully, but these errors were encountered: