Models as "views" on another model

I would like to selectively reuse some data. I have two groups of
people who want very similar information about subject experts - but
they want slightly different fields and/or want to see the other
group’s information but should not be able to edit it.

If I create the following models, don’t the child models have access
to all the parent’s attributes (unless I override the attribute
accessors individually)?

class Expert < ActiveRecord::Base

has fields like name, email, title, expertise_according_to_group1,

expertise_according_to_group2
end

class Group1_Expert < Expert

If I did this, this model would have access to all fields in Expert,

right? Including the group2 fields
end

I could do what I want by just ignoring the group2 fields when showing
the expert to group1, but that feels kind of ummm unclean.

One can use database views to provide access controls on the
underlying table. I would like to do essentially the same thing in
Ruby/Rails, but I don’t know how.

No, for models you would have to do this:

class Expert < ActiveRecord::Base
self.abstract_class = true
end

class YourChildClass < Expert

Inherits from Expert

end

Älphä Blüë wrote:

No, for models you would have to do this:

class Expert < ActiveRecord::Base
self.abstract_class = true
end

class YourChildClass < Expert

Inherits from Expert

end

It is also a good practice to set the table names in the inherited
models by doing:

class YourChildClass < Expert
set_table_name “your_child_classes”
end

Make sure you pluralize the table name…

On Jul 11, 11:19 pm, cnk [email protected] wrote:

has fields like name, email, title, expertise_according_to_group1,

expertise_according_to_group2
end

class Group1_Expert < Expert

If I did this, this model would have access to all fields in Expert,

right? Including the group2 fields
end

I could do what I want by just ignoring the group2 fields when showing
the expert to group1, but that feels kind of ummm unclean.

That is the drawback with single table inheritance, unless your
subclasses have almost all of their data in common it gets a little
icky. One approach is to move the data that is not common in to a
separate set of tables (with the appropriate relationships)

Fred

It’s not really practical to provide “access control” at the Ruby
source code level. I’d hope that you’re not giving your users access
to run code on the server, so this is really just a UI issue.

–Matt J.