Skip to content

Users cannot deprecate entries with sale agents that lack authority records #16

@demery

Description

@demery

When users attempt to deprecate entries with sale agents who have an observed name but no authority record, an exception is raised:

A NoMethodError occurred in entries#deprecate:

 undefined method `id' for nil:NilClass
 app/models/entry.rb:221:in `block in decrement_counters'
 app/models/entry.rb:220:in `each'
 app/models/entry.rb:220:in `decrement_counters'
 app/controllers/entries_controller.rb:538:in `block in deprecate'
 app/controllers/entries_controller.rb:495:in `deprecate'

The problem occurs here:

https://github.com/upenn-libraries/sdbmss/blob/master/app/models/entry.rb#L219-L224

    if sale
      sale.sale_agents.map(&:agent).uniq.each do |sale_agent|
        Name.decrement_counter(:sale_agents_count, sale_agent.id)
        objects.push(sale_agent)
      end
    end

The problem occurred with SDBM_ 8263, which has sale agents below. Note that the agent with observed_name 'Llewellyn' has a null agent_id.

mysql> select * from sale_agents where sale_id = 8257;
+--------+---------+---------------+----------+------------------+---------------------+---------------------+---------------------+------------------------+
| id     | sale_id | observed_name | agent_id | role             | created_at          | updated_at          | uncertain_in_source | supplied_by_data_entry |
+--------+---------+---------------+----------+------------------+---------------------+---------------------+---------------------+------------------------+
|  12068 |    8257 | NULL          |     7322 | selling_agent    | NULL                | NULL                |                   0 |                      0 |
|  12069 |    8257 | NULL          |    12621 | seller_or_holder | NULL                | NULL                |                   0 |                      0 |
| 160151 |    8257 | Llewellyn     |     NULL | buyer            | 2020-02-03 20:20:44 | 2020-02-03 20:20:44 |                   0 |                      0 |
+--------+---------+---------------+----------+------------------+---------------------+---------------------+---------------------+------------------------+

The problem can be corrected by skipping agents without an agent_id:

    if sale
      sale.sale_agents.map(&:agent).uniq.each do |sale_agent|
        next if sale_agent.blank? # skip if agent_id is null <= Add this line
        Name.decrement_counter(:sale_agents_count, sale_agent.id)
        objects.push(sale_agent)
      end
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions