Streamlined and STI

0 views
Skip to first unread message

hraynaud

unread,
Dec 3, 2008, 12:03:00 AM12/3/08
to Streamlined
Does Streamlined provide full support for STI?

I have following model class hierarchy

User<ActiveRecord::Base
Parent<User
Tutor<User

I have my Streamlined UI configured to Show Parents and Tutors
separately but all I seem to get back is the top level User model for
both.

Anyone tell me what I'm doing wrong?

Thanks a lot.

hraynaud

unread,
Dec 3, 2008, 12:03:00 AM12/3/08
to Streamlined

Bonefish

unread,
Dec 3, 2008, 3:19:21 AM12/3/08
to Streamlined
I've used it and it worked fine. Show me the code you have in your
model/controller/*_ui files.

Regards,
Kerry

riddim_method

unread,
Dec 3, 2008, 7:42:06 AM12/3/08
to Streamlined
Hi Thanks for the response.

Here is the code. I hope it formats correctly. Btw I don't use post
to forums much... how do you post nice formatted code, with keyword
coloring ?



module ParentAdditions

end
Parent.class_eval { include ParentAdditions }

Streamlined.ui_for(Parent) do
user_columns :login, {:link_to => {:action => "edit"}},
:email,
:status,
:spec, {:human_name=>"Info Id",:edit_in_list =>
false ,
:show_view => [:link],

},



:contact_detail, {:human_name=>"Address
Id",:edit_in_list => false,
:show_view=>[:link]
},
:students, {:human_name=>"Students",
:show_view => [:link],

}

edit_columns :login,
:email,
:status,
:enabled

end
__________________________________________________________________

module TutorAdditions

end
Tutor.class_eval { include TutorAdditions }

Streamlined.ui_for(Tutor) do
user_columns :login, {:link_to => {:action => "edit"}},
:email,
:status,
:spec, {:human_name=>"Info Id",:edit_in_list =>
false ,
:show_view => [:link],

},



:contact_detail, {:human_name=>"Address
Id",:edit_in_list => false,
:show_view=>[:link]
}


edit_columns :login,
:email,
:status,
:enabled

end

Bonefish

unread,
Dec 3, 2008, 10:48:43 AM12/3/08
to Streamlined
That looks fine. I assume your Parent and Tutor controller are
configured to use streamlined? Why don't you post the controller and
model code also.

Regards,
Kerry

riddim_method

unread,
Dec 9, 2008, 5:19:16 AM12/9/08
to Streamlined
Hi I figured out the problem but not sure how to resolve it.

The issue is the following all my user information is stored in the
user table as I mentioned in my previous post. I don't have a
separate table for my Parent and Tutor models so it looks like
ActiveRecord just grabs everything from the users table.

For example in the console executing Tutors.count an Parents.count
returns the same value.

There seems to be no clear way of filter out the appropriate data
based on which sub type is being used or is there? It would be great
if somehow you could initialize the Parent model with some conditions
based on the associated roles/and permissions so that a Parents.find
(:all) only returns parents and not users.

Basically what I'm doing in my non-admin controllers is resolving the
sub type based on roles and permissios assigned to the user (I am
using the restful authentication plugin) using before_filters etc.

Can I modify the way streamlined retrieves the records to do something
similar?

Otherwise I think I will have to change my STI model to look something
like this:

User < AcitveRecord::Base
has_one: user_login :as=>:loginable

UserLogin <ActiveRecord::Base
belongs_to :loginable, :polymorphic => true

Parent <User
#parent specific stuff here

Tutor <User
#tutor specific stuff here

Then I would basically have to modify the generated
Restful_Authentication code to work with UserLogin instead of or in
conjuction with User. I am loathe to do this because I would have to
keep doing this everytime I wanted to take advantage of a new version
of the plugin.

I am still only a couple months into rails/ruby so I am probably
missing a very simple an obvious solution to this problem.

I would appreciate any advice. Thanks

Bonefish

unread,
Dec 9, 2008, 10:59:13 AM12/9/08
to Streamlined
Streamlined/Rails supports STI. Just add a column named "type" to the
parent table and you will get the functionality you require. See the
rails docs for more info.

If you want to roll your own using an alternate differentiator you can
do something like the following. For example say you had a type_id
(rather than the rails supported "type" above) in the User table.

class Parent < User
belongs_to :type, :conditions => "types.subclass = 'Parent'"
end

class Tutor < User
belongs_to :type, :conditions => "types.subclass = 'Tutor'"
end

That's just an example. You could use your roles/permissions tables to
achieve the same effect.
Thus Parent.find(:all) will work as expected.

Regards,
Kerry

riddim_method

unread,
Dec 9, 2008, 8:01:53 PM12/9/08
to Streamlined
My God! Is there no limit to the usefulness of this thing called Ruby
On Rails?!

Thank you all very much for your help.

Bonefish, I don't know how I missed that. I guess I keep expecting
things to be really obscure and hard to do so I forget to look at some
of the obvious places.
Reply all
Reply to author
Forward
0 new messages