I wrote this scope in my Subdomain model : scope :in_account, lambda { |account_id| if account_id == "*" where("account_id >= ?", 0) else where(account_id: account_id) end } where account_id == ''*'' then all subdomain instances are selected when account_id is given , only subdomain instances in this account are selected is there a better writing ? thanks for feedback -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/3xoM8jq4ED8J. For more options, visit https://groups.google.com/groups/opt_out.
Jim Ruther Nill
2012-Nov-21 14:03 UTC
Re: is there a better DRY code writing for this scope ?
On Wed, Nov 21, 2012 at 9:33 PM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I wrote this scope in my Subdomain model : > > scope :in_account, lambda { |account_id| > if account_id == "*" > where("account_id >= ?", 0) > else > where(account_id: account_id) > end > } > > where account_id == ''*'' then all subdomain instances are selected > when account_id is given , only subdomain instances in this account are > selected > > is there a better writing ? >you can use scoped instead account_id == ''*'' ? scoped : where(account_id: account_id)> > thanks for feedback > > -- > 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 > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/3xoM8jq4ED8J. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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 https://groups.google.com/groups/opt_out.
Great ! Thanks Jim Le mercredi 21 novembre 2012 15:04:57 UTC+1, jim a écrit :> > > > > On Wed, Nov 21, 2012 at 9:33 PM, Erwin <yves_...-ee4meeAH724@public.gmane.org <javascript:>>wrote: > >> I wrote this scope in my Subdomain model : >> >> scope :in_account, lambda { |account_id| >> if account_id == "*" >> where("account_id >= ?", 0) >> else >> where(account_id: account_id) >> end >> } >> >> where account_id == ''*'' then all subdomain instances are selected >> when account_id is given , only subdomain instances in this account are >> selected >> >> is there a better writing ? >> > > you can use scoped instead > > account_id == ''*'' ? scoped : where(account_id: account_id) > > >> >> thanks for feedback >> >> -- >> 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 rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> >> . >> To unsubscribe from this group, send email to >> rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/3xoM8jq4ED8J. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.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. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Y2teFjkW-McJ. For more options, visit https://groups.google.com/groups/opt_out.
Dave Aronson
2012-Nov-21 18:17 UTC
Re: is there a better DRY code writing for this scope ?
On Wed, Nov 21, 2012 at 8:33 AM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I wrote this scope in my Subdomain model : > > scope :in_account, lambda { |account_id| > if account_id == "*" > where("account_id >= ?", 0) > else > where(account_id: account_id) > end > } > > where account_id == ''*'' then all subdomain instances are selected > when account_id is given , only subdomain instances in this account are > selected > > is there a better writing ?Try: scope :in_account, lambda { |account_id| where(account_id: account_id) if account_id != "*" } If the conditional fails, this will just not interfere with the query. If it''s possible that account_id will be nil or blank (as on a page accessed w/o passing any params), tack "&& account_id.present?" onto the conditional. -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- 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 https://groups.google.com/groups/opt_out.