This post originated from an RSS feed registered with Ruby Buzz
by Jason Perry.
Original Post: Exclusive Conditions for ActiveRecord's has_many
Feed Title: Ambethia
Feed URL: http://ambethia.com/feed
Feed Description: The journal and personal website of Jason L Perry, dealing in development, design and distractions.
I’ll start with some code.
class User < ActiveRecord::Base
has_many :things, :exclusive_conditions => %q(`things`.user_id = #{id} OR #{admin?})
end
Assuming, we have 2 users, Alice and Bob.
user = User.find_by_username("alice") # => Alice is an admin.
user.id # => 1
thing = user.things.find_by_name("Bob's Thing")
thing.user_id # => 2, It's still bobs thing.
Now in my controller’s I can just say something like:
class [...]