Help with STI and accepts_nested_attributes_for

The STI for Hosts (Host::Managed) breaks the accepts_nested_attributes_for

This error in the current 3.0.20 "develop" branch and the new 3.2.13 branch.

When a hostgroup for host is changed, the Parameters tab show "Loading parameters…" and doesn't load

The error is

HostParameter Load (0.7ms) SELECT "parameters".* FROM "parameters" WHERE "parameters"."type" IN ('HostParameter') AND "parameters"."reference_id" IS NULL AND "parameters"."id" IN (7)
ActiveRecord::RecordNotFound: Couldn't find HostParameter with ID=7 for Host::Managed with ID=

The error is caused by Host.new on hosts_controller.rb#460

I can duplicate this error in the console by

1.9.3p385 :054 > params = {"name"=>"fdev", "hostgroup_id"=>"23", "environment_id"=>"1", "puppetclass_ids"=>[""], "managed"=>"t", "progress_report_id"=>"52e4128d-560d-47f1-989f-9a9d9698ee60", "mac"=>"52:54:00:05:49:de", "domain_id"=>"6", "ip"=>"10.35.27.3", "updated_at"=>"1364484907", "interfaces_attributes"=>{"new_interfaces"=>{"_destroy"=>"false", "type"=>"Nic::Managed", "mac"=>"", "name"=>"", "domain_id"=>"", "ip"=>"staff@kehila.org", "provider"=>"IPMI"}}, "architecture_id"=>"", "provision_method"=>"build", "disk"=>"", "root_pass"=>"123456", "host_parameters_attributes"=>{"0"=>{"name"=>"RESERVED", "value"=>"false", "_destroy"=>"false", "nested"=>"", "id"=>"7"}}, "is_owned_by"=>"", "enabled"=>"1", "model_id"=>"2", "comment"=>"", "overwrite"=>"false"}

1.9.3p385 :055 > Host.new(params)
HostParameter Load (0.7ms) SELECT "parameters".* FROM "parameters" WHERE "parameters"."type" IN ('HostParameter') AND "parameters"."reference_id" IS NULL AND "parameters"."id" IN (7)
ActiveRecord::RecordNotFound: Couldn't find HostParameter with ID=7 for Host::Managed with ID=

It seems to be an STI issue with the param host_parameters_attributes

In managed.rb

has_many :host_parameters, :dependent => :destroy, :foreign_key => :reference_id
accepts_nested_attributes_for :host_parameters, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true

Btw, for the example data, there is a host_parameter with id = 7

1.9.3p385 :058 > Host.find(394).host_parameters
Host::Managed Load (0.8ms) SELECT "hosts".* FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = $1 LIMIT 1 [["id", 394]]
HostParameter Load (0.5ms) SELECT "parameters".* FROM "parameters" WHERE "parameters"."type" IN ('HostParameter') AND "parameters"."reference_id" = 394

··· +----+----------+-------+--------------+-------------------------+-------------------------+---------------+----------+ > id | name | value | reference_id | created_at | updated_at | type | priority | +----+----------+-------+--------------+-------------------------+-------------------------+---------------+----------+ > 7 | RESERVED | false | 394 | 2013-01-21 15:31:18 UTC | 2013-01-21 15:36:31 UTC | HostParameter | 4 | +----+----------+-------+--------------+-------------------------+-------------------------+---------------+----------+

> The STI for Hosts (Host::Managed) breaks the accepts_nested_attributes_for
>
> This error in the current 3.0.20 "develop" branch and the new 3.2.13branch.
>
> When a hostgroup for host is changed, the Parameters tab show "Loading
> parameters…" and doesn't load
>
> The error is
>
> HostParameter Load (0.7ms) SELECT "parameters".* FROM "parameters" WHERE
> "parameters"."type" IN ('HostParameter') AND "parameters"."reference_id" IS
> NULL AND "parameters"."id" IN (7)
> ActiveRecord::RecordNotFound: Couldn't find HostParameter with ID=7 for
> Host::Managed with ID=
>
> The error is caused by Host.new on hosts_controller.rb#460
>
> I can duplicate this error in the console by
>
> 1.9.3p385 :054 > params = {"name"=>"fdev", "hostgroup_id"=>"23",
> "environment_id"=>"1", "puppetclass_ids"=>[""], "managed"=>"t",
> "progress_report_id"=>"52e4128d-560d-47f1-989f-9a9d9698ee60",
> "mac"=>"52:54:00:05:49:de", "domain_id"=>"6", "ip"=>"10.35.27.3",
> "updated_at"=>"1364484907",
> "interfaces_attributes"=>{"new_interfaces"=>{"_destroy"=>"false",
> "type"=>"Nic::Managed", "mac"=>"", "name"=>"", "domain_id"=>"", "ip"=>"
> staff@kehila.org", "provider"=>"IPMI"}}, "architecture_id"=>"",
> "provision_method"=>"build", "disk"=>"", "root_pass"=>"123456",
> "host_parameters_attributes"=>{"0"=>{"name"=>"RESERVED", "value"=>"false",
> "_destroy"=>"false", "nested"=>"", "id"=>"7"}}, "is_owned_by"=>"",
> "enabled"=>"1", "model_id"=>"2", "comment"=>"", "overwrite"=>"false"}
>
> 1.9.3p385 :055 > Host.new(params)
> HostParameter Load (0.7ms) SELECT "parameters".* FROM "parameters"
> WHERE "parameters"."type" IN ('HostParameter') AND
> "parameters"."reference_id" IS NULL AND "parameters"."id" IN (7)
> ActiveRecord::RecordNotFound: Couldn't find HostParameter with ID=7 for
> Host::Managed with ID=
>
> It seems to be an STI issue with the param host_parameters_attributes
>
> In managed.rb
>
> has_many :host_parameters, :dependent => :destroy, :foreign_key =>
> :reference_id
> accepts_nested_attributes_for :host_parameters, :reject_if => lambda {
> >a> a[:value].blank? }, :allow_destroy => true
>

yes, if you look at the other side of the relationship in
app/mode/host_parameter.rb its wrong

  • belongs_to :host, :foreign_key => :reference_id
  • belongs_to_host :foreign_key => :reference_id

Ohad

··· On Thu, Mar 28, 2013 at 6:04 PM, Joseph Magen wrote:

Btw, for the example data, there is a host_parameter with id = 7

1.9.3p385 :058 > Host.find(394).host_parameters
Host::Managed Load (0.8ms) SELECT “hosts”.* FROM “hosts” WHERE
"hosts".“type” IN (‘Host::Managed’) AND “hosts”.“id” = $1 LIMIT 1 [[“id”,
394]]
HostParameter Load (0.5ms) SELECT “parameters”.* FROM “parameters"
WHERE “parameters”.“type” IN (‘HostParameter’) AND
"parameters”.“reference_id” = 394

±—±---------±------±-------------±------------------------±------------------------±--------------±---------+

id | name | value | reference_id | created_at |
updated_at | type | priority |

±—±---------±------±-------------±------------------------±------------------------±--------------±---------+

7 | RESERVED | false | 394 | 2013-01-21 15:31:18 UTC |
2013-01-21 15:36:31 UTC | HostParameter | 4 |

±—±---------±------±-------------±------------------------±------------------------±--------------±---------+


You received this message because you are subscribed to the Google Groups
"foreman-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

ok, so following up on the next issue here is this.

when editing a host, and then changing a hostgroup (or env), the classes
list and parameters needs to be refreshed.

until now, the way its done, is that all of the form attributes are sent
over, and at foreman side you have
host = Host.new params[:host]

this is create, as it create a temp in memory obj without needing to fetch
all data from the db, the downside ,that some queries like
host.parameters will generate the wrong query, as the new host obj has no
ID resulting with something odd like:

SELECT "parameters".* FROM "parameters" WHERE "parameters"."type" IN
('HostParameter') AND "parameters"."reference_id" IS NULL AND
"parameters"."id" IN (101)

as host is without an ID.

Ohad

··· On Thu, Mar 28, 2013 at 9:45 PM, Ohad Levy wrote:

On Thu, Mar 28, 2013 at 6:04 PM, Joseph Magen jmagen@redhat.com wrote:

The STI for Hosts (Host::Managed) breaks the accepts_nested_attributes_for

This error in the current 3.0.20 “develop” branch and the new 3.2.13branch.

When a hostgroup for host is changed, the Parameters tab show “Loading
parameters…” and doesn’t load

The error is

HostParameter Load (0.7ms) SELECT “parameters”.* FROM “parameters” WHERE
"parameters".“type” IN (‘HostParameter’) AND “parameters”.“reference_id” IS
NULL AND “parameters”.“id” IN (7)
ActiveRecord::RecordNotFound: Couldn’t find HostParameter with ID=7 for
Host::Managed with ID=

The error is caused by Host.new on hosts_controller.rb#460

I can duplicate this error in the console by

1.9.3p385 :054 > params = {“name”=>“fdev”, “hostgroup_id”=>“23”,
“environment_id”=>“1”, “puppetclass_ids”=>[""], “managed”=>“t”,
“progress_report_id”=>“52e4128d-560d-47f1-989f-9a9d9698ee60”,
“mac”=>“52:54:00:05:49:de”, “domain_id”=>“6”, “ip”=>“10.35.27.3”,
“updated_at”=>“1364484907”,
“interfaces_attributes”=>{“new_interfaces”=>{"_destroy"=>“false”,
“type”=>“Nic::Managed”, “mac”=>"", “name”=>"", “domain_id”=>"", “ip”=>“
staff@kehila.org”, “provider”=>“IPMI”}}, “architecture_id”=>"",
“provision_method”=>“build”, “disk”=>"", “root_pass”=>“123456”,
“host_parameters_attributes”=>{“0”=>{“name”=>“RESERVED”, “value”=>“false”,
"_destroy"=>“false”, “nested”=>"", “id”=>“7”}}, “is_owned_by”=>"",
“enabled”=>“1”, “model_id”=>“2”, “comment”=>"", “overwrite”=>“false”}

1.9.3p385 :055 > Host.new(params)
HostParameter Load (0.7ms) SELECT “parameters”.* FROM “parameters"
WHERE “parameters”.“type” IN (‘HostParameter’) AND
"parameters”.“reference_id” IS NULL AND “parameters”.“id” IN (7)
ActiveRecord::RecordNotFound: Couldn’t find HostParameter with ID=7 for
Host::Managed with ID=

It seems to be an STI issue with the param host_parameters_attributes

In managed.rb

has_many :host_parameters, :dependent => :destroy, :foreign_key =>
:reference_id
accepts_nested_attributes_for :host_parameters, :reject_if => lambda {

a> a[:value].blank? }, :allow_destroy => true

yes, if you look at the other side of the relationship in
app/mode/host_parameter.rb its wrong

  • belongs_to :host, :foreign_key => :reference_id
  • belongs_to_host :foreign_key => :reference_id

Ohad

Btw, for the example data, there is a host_parameter with id = 7

1.9.3p385 :058 > Host.find(394).host_parameters
Host::Managed Load (0.8ms) SELECT “hosts”.* FROM “hosts” WHERE
"hosts".“type” IN (‘Host::Managed’) AND “hosts”.“id” = $1 LIMIT 1 [[“id”,
394]]
HostParameter Load (0.5ms) SELECT “parameters”.* FROM “parameters"
WHERE “parameters”.“type” IN (‘HostParameter’) AND
"parameters”.“reference_id” = 394

±—±---------±------±-------------±------------------------±------------------------±--------------±---------+

id | name | value | reference_id | created_at |
updated_at | type | priority |

±—±---------±------±-------------±------------------------±------------------------±--------------±---------+

7 | RESERVED | false | 394 | 2013-01-21 15:31:18 UTC |
2013-01-21 15:36:31 UTC | HostParameter | 4 |

±—±---------±------±-------------±------------------------±------------------------±--------------±---------+


You received this message because you are subscribed to the Google Groups
"foreman-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.