I''m trying to get more into the Ruby mindset. I know I should accomplish more with less code. I came up with this method that does work, but I think could really use some simplification. There is 4 models. Account Company (belongs to Account) User (belongs to Company) (belongs to Role) (to get the account id, I would need to do user.company.account_id) Role User.role_id (if it''s a 1, it means it''s the Account Owner) There is only ONE account owner per account. So, when a "customer"(role_id=4) signs in, under the Users tab, he''s supposed to see two companies: 1) the company he belongs to 2) and the account owners company So I need to come up with something like this: @companies = Company.all(:conditions => ["id == ? or id == ?", current_user.company_id, account_owner_company_id]) 1) Coming up with the company he belongs to it''s easy: current_user.company_id 2) Coming up with the account owner company id it''s more difficult and requires several queries. The method I have does the following: a) Finds the customer''s account_id b) Looks for ALL account owners in the database c) Tries to match the customer''s account_id to one of the account owners id''s d) Finally, returns the account owner''s company id --------------------------------------------------------------- def account_owner_company_id customer_account_id = current_user.company.account_id account_owners = User.find_all_by_role_id(1) account_owners.each do |ao| if ao.company.account_id == customer_account_id @this_one = ao.company_id end end @this_one end --------------------------------------------------------------- You can easily see that if the database grows, looking for ALL account owners in the database can take up a lot of resources. Can you suggest an easier way to find the account owner company id when being logged in as a customer? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 10, 7:54 pm, "Leonel *.*" <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> --------------------------------------------------------------- > > You can easily see that if the database grows, looking for ALL account > owners in the database can take up a lot of resources. Can you suggest > an easier way to find the account owner company id when being logged in > as a customer? >Your data structure feels a little weird to me - Presumably accounts have multiple companies. So does someone with role_id == 1 own all the companies in the account that the company_id points to? It feels a little lopsided to have this one privileged company. I think personally I would give account an owner_id attribute. Anyway, assuming that users will grow rapidly but that an account will only ever have a manageable number of companies in it, something like all_companies_in_account = current_user.company.account.companies owner = User.find_by_role_id_and_company_id(1, all_companies_in_account) would find you the owner. Fred> -- > Posted viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Fred! The reason of this setup (might need some adjusting) is that an Account has an attribute paying_method. So only the account_owner is the one that pays. The account owner can add, edit or delete Companies and Users. He''s the administrator for that Account. The Users under the account owner''s Company, are kinda like managers or agents. The Users under other than the account owner''s company are customers. __________________________________________ ACCOUNT OWNER AND AGENTS __________________________________________ So let''s say Tom is the account owner. His company is Landscaping Solutions. He pays the app. ----------------------------------------------------- Users under his company (agents) can open, modify and close tickets. Tom (user, role: account owner) Rick (user, role: agent) Pam (user, role:agent) ----------------------------------------------------- ___________________________________________________ CUSTOMERS ____________________________________________________ So Tom invites other creates companies and invites users. Customers can only open tickets. ---------------------------------------------------- Goliath National Bank (company) Barney (user, role: customer) Marshall (user, role: customer) Mobious Designs (company) Ted (user, role: customer) ------------------------------------------------------ -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think you can dispense with Company altogether and just have an association class describing a User''s relationship to an Account: ``` Account has_many :users, :through => :memberships User belongs_to :account Membership belongs_to :user belongs_to :account has_one :role # agent, owner, etc. ``` ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Wed, Aug 10, 2011 at 16:20, Leonel *.* <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks Fred! > > The reason of this setup (might need some adjusting) is that an Account > has an attribute paying_method. So only the account_owner is the one > that pays. > > The account owner can add, edit or delete Companies and Users. He''s the > administrator for that Account. > > The Users under the account owner''s Company, are kinda like managers or > agents. > > The Users under other than the account owner''s company are customers. > > __________________________________________ > ACCOUNT OWNER AND AGENTS > __________________________________________ > So let''s say Tom is the account owner. > His company is Landscaping Solutions. > He pays the app. > ----------------------------------------------------- > Users under his company (agents) can open, modify and close tickets. > Tom (user, role: account owner) > Rick (user, role: agent) > Pam (user, role:agent) > ----------------------------------------------------- > > ___________________________________________________ > CUSTOMERS > ____________________________________________________ > So Tom invites other creates companies and invites users. > Customers can only open tickets. > ---------------------------------------------------- > Goliath National Bank (company) > Barney (user, role: customer) > Marshall (user, role: customer) > > Mobious Designs (company) > Ted (user, role: customer) > ------------------------------------------------------ > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sorry, typo. Should be: ``` Account has_many :users, :through => :memberships User has_many :accounts, :through => :memberships Membership belongs_to :user belongs_to :account has_one :role # agent, owner, etc. ``` -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Wed, Aug 10, 2011 at 18:15, John Feminella <johnf-u89qwezJ71hz+5FpPkU+UQ@public.gmane.org> wrote:> I think you can dispense with Company altogether and just have an > association class describing a User''s relationship to an Account: > > ``` > Account > has_many :users, :through => :memberships > > User > belongs_to :account > > Membership > belongs_to :user > belongs_to :account > has_one :role # agent, owner, etc. > ``` > > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > > > On Wed, Aug 10, 2011 at 16:20, Leonel *.* <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Thanks Fred! >> >> The reason of this setup (might need some adjusting) is that an Account >> has an attribute paying_method. So only the account_owner is the one >> that pays. >> >> The account owner can add, edit or delete Companies and Users. He''s the >> administrator for that Account. >> >> The Users under the account owner''s Company, are kinda like managers or >> agents. >> >> The Users under other than the account owner''s company are customers. >> >> __________________________________________ >> ACCOUNT OWNER AND AGENTS >> __________________________________________ >> So let''s say Tom is the account owner. >> His company is Landscaping Solutions. >> He pays the app. >> ----------------------------------------------------- >> Users under his company (agents) can open, modify and close tickets. >> Tom (user, role: account owner) >> Rick (user, role: agent) >> Pam (user, role:agent) >> ----------------------------------------------------- >> >> ___________________________________________________ >> CUSTOMERS >> ____________________________________________________ >> So Tom invites other creates companies and invites users. >> Customers can only open tickets. >> ---------------------------------------------------- >> Goliath National Bank (company) >> Barney (user, role: customer) >> Marshall (user, role: customer) >> >> Mobious Designs (company) >> Ted (user, role: customer) >> ------------------------------------------------------ >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.