Closed ryanburnette closed 10 years ago
Do I need to create an index for type when using STI?
You don't need to, but I would probably out one on the type and id combination.
On Sunday, February 9, 2014, Ryan Burnette notifications@github.com wrote:
Do I need to create an index for type when using STI?
Reply to this email directly or view it on GitHubhttps://github.com/ryanburnette/rcms/issues/14#issuecomment-34592598 .
-- Paul
Roger that. Thanks.
On Sun, Feb 9, 2014 at 7:34 PM, Paul Elliott notifications@github.comwrote:
You don't need to, but I would probably out one on the type and id combination.
On Sunday, February 9, 2014, Ryan Burnette notifications@github.com wrote:
Do I need to create an index for type when using STI?
Reply to this email directly or view it on GitHub< https://github.com/ryanburnette/rcms/issues/14#issuecomment-34592598> .
-- Paul
— Reply to this email directly or view it on GitHubhttps://github.com/ryanburnette/rcms/issues/14#issuecomment-34593102 .
I hadn't read anything about this in documentation, but it appears that STI doesn't mean that you inherit the relationships. If I set up a habtm relationship between Asset and Post, and Page is inheriting from Post, it doesn't seem to inherit the relationship with Asset. I assume I'd have to define that relationship between Asset and Page explicitly and create another join table for each inherited type.
If you are subclassing the parent model then you should receive all the same associations.
I must be doing something wrong. I'll review this.
@paulelliott I just played around with this a bit and I'm not sure what I've missed.
The Post model has a many_to_many association with the Image model which works fine for Post. https://github.com/ryanburnette/rcms/blob/master/app/models/post.rb
The Page model inherits from Post, but the images
method is not defined for it.
https://github.com/ryanburnette/rcms/blob/master/app/models/page.rb
2.1.0 :016 > Post.first.images
Post Load (0.4ms) SELECT `posts`.* FROM `posts` ORDER BY `posts`.`id` ASC LIMIT 1
Image Load (0.4ms) SELECT `images`.* FROM `images` INNER JOIN `images_posts` ON `images`.`id` = `images_posts`.`image_id` WHERE `images_posts`.`post_id` = 1
=> #<ActiveRecord::Associations::CollectionProxy []>
2.1.0 :017 > Page.first.images
Page Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`type` IN ('Page') ORDER BY `posts`.`id` ASC LIMIT 1
NoMethodError: undefined method `images' for nil:NilClass
from (irb):17
from /Users/ryan/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/ryan/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/ryan/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.0 :018 >
Any advice on what to look into to troubleshoot? I just assumed when I first saw this that the the associations were not inherited.
There error you posted isn't saying that the association isn't there. It is saying that Page.first
returned a nil
. Try doing Page.new.images
and see what it says.
So the weirdness was that Page.first
was returning nil
. I don't know how that was possible as there were several pages in existence. I removed them all, added a few new pages and it behaves as it should. Strange.
Another thing I noticed is that Post.all
return all posts and the inherited type objects. I had to change my methods in PostController
to make them specific to just posts. If I had to do it over I would create a model for Post
and Page
inherit from so they'd both have their types explicitly defined. I can think of other cases where the natural behavior of STI, however, would be desirable.
STI works now. I have Page inheriting from Post.