May
20th
Tue
20th
Rails tip if you use association proxy methods
I like doing following in my code:class Blah < ActiveRecord::Base
has_many :somethings
end
class Something < ActiveRecord::Base
def self.for(person)
find(:first, :conditions => {:owner_name => person})
end
end
# And then later
@the_blah.somethings.for("dima") # => #<Something ....>
But I also can accidentally do this and break the expectation for the code, since I don’t have full conditions specified in the Something.for method:
Something.for("dima")
class Something < ActiveRecord::Base
def self.for(person)
raise unless scoped?(:find)
find(:first, :conditions => {:owner_name => person})
end
end
