Skip to content
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

Failure to find instances of subclasses #8

Open
wmarbut opened this issue Jan 4, 2012 · 0 comments
Open

Failure to find instances of subclasses #8

wmarbut opened this issue Jan 4, 2012 · 0 comments

Comments

@wmarbut
Copy link

wmarbut commented Jan 4, 2012

Summary

When an instance of a subclass is created and none of the fields specific to that class are entered, the object cannot be queried

Situations where this is a problem

Some subclasses may only contain foreign key fields. For example, the Parent class in the prerequisites section could only contain the children field. This bug prevents the Parent class from being a persistable object.

Potential fix

I haven't really tested this and it is late for me, but this might work

Add this after line 153 of acts_as_cities.rb

        if (attributes_for_part_of.empty?) 
          attributes_for_part_of["id"] = self.id
        end

Prerequisites

Suppose the following classes

  • Person
    • name (string)
    • birthday (datetime)
  • Parent (extends Person)
    • children (foreign key)
    • can_yodel (int)
  • Child (extends Person)
    • parents (foreign key)

Failure use-case

If you create a new parent object, but fail to set any attributes specific to the parent table, then It will not show up when queried

parent = Parent.new
parent.name = 'Shepherd Book'
parent.birthday = Time.now 
parent.save

When you try to query for this item, it is inaccessible

Parent.where(:name => 'Shepherd Book')

returns

 => [] 

Working use-case

When you create an instance of a subclass and use one or more of the fields specific to that subclass you can query for it

parent = Parent.new
parent.name = 'Shepherd Book'
parent.birthday = Time.now
parent.can_yodel = 1 
parent.save

returns

=> [#<Parent id: 1, type: "Parent", name: "Shepherd Book", birthday: "2012-01-03 05:00:00", can_yodel: 1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant