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
on 2012-11-21 14:34
on 2012-11-21 15:05
On Wed, Nov 21, 2012 at 9:33 PM, Erwin <yves_dufour@mac.com> wrote: > 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) > https://groups.google.com/d/msg/rubyonrails-talk/-.... > For more options, visit https://groups.google.com/groups/opt_out. > > > --
on 2012-11-21 19:19
On Wed, Nov 21, 2012 at 8:33 AM, Erwin <yves_dufour@mac.com> wrote: > 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/.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.