The example is:
Conference has many participants
>From a Conference entry I want to link to a listing of all the
participants. The route would be:
map.connect 'legs_for/:conference_id',
:controller => "participant",
:action => "list"
and I want the list action to check for the parameter conference_id
and if it exists limit the list to those participants that meet that
criteria, something like:
Participant.find(:all, :conditions => ["conference_id =
#{params[:conference_id]}"
Is there a way to do something like this?
Is there a way to pass in :conditions to the finds being used in the
CrudMethods::list ?
Thanks!
http://streamlinedframework.org:8079/trac/
Matthew
In the mean time I did accomplish my goal using the around_filter
with_scope kludge:
In application.rb:
class ApplicationController < ActionController::Base
# Pick a unique cookie name to distinguish our session data from
others'
session :session_key => '_zipdx_dash_session_id'
def filter_legs_by_conference_id
return yield if params[:conference_id].nil?
Leg.with_scope( :find => { :conditions => ["conference_id =
#{params[:conference_id]}"]}, :create => {:conference_id =>
params[:conference_id] } ) do
yield
end
end
end
Then in my LegController (the controller associated with the model
that contains the "belongs_to" declaration) I added:
around_filter :filter_legs_by_conference_id
My example uses Calculations and Users:
class Calculation < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :calculations
has_many :misc_calculations, :class_name =>
'Calculation', :conditions => 'project_id = 0 OR project_id IS NULL'
end
class CalculationsController < ApplicationController
layout 'streamlined'
acts_as_streamlined
before_filter :get_user_collection
def misc
@collection = @user.misc_calculations
# Call into the Streamlined list action.
# (We could just use list, if we haven't messed w/ it.)
streamlined_list
end
private
alias :streamlined_list :list
def get_user_collection
@user = User.find(params[:user])
@collection = @user.calculations
end
def model
@collection
end
end
There are a few subtle tricks involved here. First, we override
Streamlined's 'model' method. This would normally return the
Calculations class object itself. But Streamlined only uses a few
methods on the object, and it turns out that AR association proxies
implement basically the same methods as the AR class objects. So we
can use @user.calculations in place of Calculations. In my 'misc'
action, I added some more conditions using another named/conditioned
association (I couldn't find a name for this, but here's a great
article on it: http://www.adambondurant.com/2007/5/3/easier-association-proxies).
So we can use that in place of Calculations as well.
I'd like some feedback on this technique. I haven't tested it too
much, but it's working great for my needs. It even prevents users from
editing items that don't belong to them. I haven't tested it with
HABTM or HMT yet. I don't see why it wouldn't work, although looking
through the AR association proxy code for HABTM, I think it may be
missing the count() method that Streamlined needs. It shouldn't be too
hard to add that though. (Either by monkey-patching AR, or extending
the association as in the first example in the URL I referenced
above.)
Enjoy,
Craig
can use @ user.calculations in place of Calculations. In my 'misc'
Yes. I've tested it with all of those, plus listing, editing/updating,
creating, and showing.
Craig