STI include in find

Hi,

I am using single table inheritance in my application as follows:

class Company < ActiveRecord::Base

end

class Member < Company

end

class Applicant < Company

end

class Application < ActiveRecord::Base
has_one :applicant
has_one :member

end

I wish to use an include statement when I do a find on the Application
(ie. Application.find(:all, :include => [:member]). Is there a way to
accomplish this. I’m assuming it is not working because of the STI.

Thanks for your help.

Hey Lundie,

That’s odd…this sounded weird to me so I setup a sample app and tested
it.
It works as expected (note the 2 mysql calls the find generates, as you
would expect):

ActiveRecord#explain is added by the (very young) explain-query gem

Post.explain do
?> Post.find :first, :include => [:user]

end
SELECT * FROM posts LIMIT 1
select_type | key_len | table | id | possible_keys | type | Extra |
rows
| ref | key


SIMPLE | | posts | 1 | | ALL | | 6
| |

SELECT entities.* FROM entities WHERE (entities.post_id = 2) AND
(
(entities.type = ‘User’ ) )
select_type | key_len | table | id | possible_keys | type |
Extra | rows | ref | key


SIMPLE | | entities | 1 | | ALL | Using
where | 2 | |

=> #<Post id: 2, title: “First Post”, content: “some content\r\n”,
created_at: “2009-07-07 09:19:46”, updated_at: “2009-07-20 14:16:16”,
permalink: “first-post”, comments_count: 10>

Is your STI working correctly in other use cases?

Regards,
Gustav P.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs