Wasn't named_scope a plugin before it was in 2.1?
On Aug 14, 1:35 pm, ben wiseley wrote:
> I screwed up my example if it matters... should have gone more like:
>
> Class Area < ActiveRecord::Base
> def taxes
> return .1
> end
>
> def calc_price(amount)
> amount + (amount * determi
I screwed up my example if it matters... should have gone more like:
Class Area < ActiveRecord::Base
def taxes
return .1
end
def calc_price(amount)
amount + (amount * determine_taxes)
end
end
class InnerCity < Area
def self.find(*args)
super(args.first,
args.extract_option
Thanks everyone for all your suggestions. I'm using Ben's method as it works
best for my scenario, but I'll file away the techniques for future use.
My old rails doesn't have extract_options, so I broke this up into a few lines:
options = args.last.is_a?(Hash) ? args.last : {}
options =
I think it really depends on what's needed. If you simply need to filter
rows then, yes, the solution below is good. But, if you need custom logic
for InnerCity vs Rural then I'd sub class the models like you originally
suggested.
Class Area < ActiveRecord::Base
def taxes
return .1
end
You could achieve what you want with STI, write a before_save callback
that changes the type to 'InnerCity' when you save a Location with a
postcode within the 2000, 2001 range. It feels wrong to me but it
should work.
named_scopes would be nicer for this. It used to be a plugin
(has_finde
It would go in the model.
class Location < ActiveRecord::Base
def self.all_from_inner_city
find(:all, :conditions=>{:postcode=>[2001, 2002]})
end
end
Location.all_from_inner_city
or
class Location < ActiveRecord::Base
def self.from_inner_city(*args)
find(args.first,
(args.extra
That would be in the controller not the model, wouldn't it?
Whilst I could do that I really want a different model, so I can
implement different logic for InnerCity locations (eg increase the
price, decrease the size, ... :-) ).
Sonia Hamilton.
Jonathan Clarke wrote:
> Something like this?
>
I was about to suggest that. But it's Rails 1.2.3. I don't recall that
being available back then.
Neither was named_scopes, if I recall correctly.
My suggestion: don't subclass it. Have a class method in Location
perhaps called all_from_inner_city which runs the finder suggested by
Jonathan Clar
Sonia Hamilton wrote:
> My question is: if I subclass a model, how do I limit the records
> retrieved in the subclass?
I think The Rails Way to do this is to use a named scope, and not a
subclass. Eg:
class Location < ActiveRecord::Base
named_scope :inner_city, :conditions => {:postcode
Hi Sonia,
I'd recommend looking at default_scope.
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002313
Cheers,
Dylan.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" group.
To
Something like this?
@foo = InnerCity.find(:all, :conditions=>{:postcode=>[2001, 2002]})
Jonathan
2009/8/14 Sonia Hamilton :
>
> Hi guys, I've been lurking around the RORO list for a while. I don't
> know whether it's appropriate to ask rails tech questions here, but I'll
> post anyway and see
11 matches
Mail list logo