I'm not quite sure what to do here, here's the situation:
Given these models...
class Post < ActiveRecord::Base
has_many :post_assets
has_many :assets, :through => :post_assets
acts_as_scopeable :assets, :scopes => [:featured]
end
class PostAssets < ActiveRecord::Base
belongs_to :post
belongs_to :asset
# context is so we know the scope or role
# the join plays
validates_presences_of :context
end
class Asset < ActiveRecord::Base
has_many :post_assets
has_many :posts, :through => :post_assets
end
How do I make it so I can do something like this:
post = Post.create!(:title => "Post Title")
asset = Asset.create!(:title => "An Asset", :context => "featured")
post.assets << asset
post.reload
post.featured_assets #=> [#<Asset id: 1, title: "An Asset", created_at: "2010-06-20 16:30:51", updated_at: "2010-06-20 16:30:51">]
Assume the acts_as_scopeable
method just dynamically defines the featured_assets
methods (and whatever others you add).
What's confusing me is that it seems like I can do it in the following different ways:
acts_as_scopeable
dynamically defines simple methods, and in there I do some sort of customfind
on the associationassets
, likeassets.find(:conditions => {:scope => passed_scope}}
. That doesn't seem optimized though.- I could also define class
named_scopes
that do what #1 does, and then call the named scope, which just pushes of the problem.
What is the recommended way to do this, or some docs I can read to get started. Thanks!