script/generate migration add_roles_mask_to_users roles_mask:integer
rake db:migrate
# in models/user.rb
ROLES = %w[Manager Worker]
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def roles
ROLES.reject do |r|
((roles_mask || 0) & 2**ROLES.index(r)).zero?
end
end
def is?(role)
roles.include?(role.to_s)
end
Or install the gem that does this http://rubygems.org/gems/role_model
On Sat, Sep 11, 2010 at 11:07 PM, Daniel Gaytán <
[email protected]> wrote:
> I forgot to mention that you must to define a default scope for getting
> first all Managers or Workers in each case and a before create filter to set
> the role the person you are creating will have or whatever:
>
> #manager.rb
>
> before_create :set_manager_role
>
> default_scope :where => {:role => "manager"}
>
> private
>
> def set_manager_role
> self.role = "manager"
> end
>
> The same for the worker model
>
>
> 2010/9/11 Daniel Gaytán <[email protected]>
>
> I think this could work:
>>
>> class Task < ActiveRecord::Base
>> belongs_to :worker
>> belongs_to :manager
>> end
>>
>> class Person < ActiveRecord::Base
>> end
>>
>> class Manager < Person
>> has_many :tasks
>> end
>>
>> class Worker < Person
>> has_many :tasks
>> end
>>
>> task = Task.create
>> manager = Manager.create
>> worker = Worker.create
>>
>> task.manager = manager
>> task.worker = worker
>>
>>
>> You don't need to create two tables, only to files more... extending the
>> model that is related to the table.
>>
>> Daniel Gaytán
>>
>> 2010/9/11 Brian Ablaza <[email protected]>
>>
>> I have a People table and a Tasks table. Some People are Managers, some
>>> are Workers. Each Task has a Manager and a Worker.
>>>
>>> How do I define the relationships? I tried a STI setup, where Managers
>>> and Workers inherit from People, and People has a type column. In my
>>> Task table, I have worker_id and manager_id rows. Then:
>>>
>>> task belongs_to worker
>>> task belongs_to manager
>>> worker has_many tasks
>>> manager has_many tasks
>>>
>>> But when I ask for task.worker, or task.manager, it throws an error.
>>>
>>> How can I do this without separate Worker and Manager tables?
>>> --
>>> Posted via http://www.ruby-forum.com/.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Ruby on Rails: Talk" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<rubyonrails-talk%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/rubyonrails-talk?hl=en.
>>>
>>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.