Hi Guys, ActiveRecord is great for being able to drill down through many tables. I often locate the very "top" record, and have a render :action => "show" display that top record. Often I want to drill down like: @kingdom.phylums.classes.orders.each do |order| render :partial => "order/show" end But, depending on the user who is logged in, they see a different set of "order", or a different set of "phylums". In other words, as I drill down, I want to filter what the result set (has_many) by data not in the ActiveRecord. I want to filter by data that is from a web form, from ajax, or from session variables. ActiveRecord makes it easy enough to do so with data in the record such as: has_many :phylums, :finder_sql => "blah blah blah where kingdom_id = #{id} " But it can''t do something like has_many :authorized_orders, :conditions => "order_owned_by = session[:current_user]" I tried to get around this by thinking of: render :partial => "call another action" But render to my knowledge can''t really go calling another ActionController to render more data. So I''m kind of stuck... there''s no easy way to filter by external data, and there''s no easy way to break out of an ActionView and include output from an ActionController. I don''t want to go totally breaking this "MVC" idea by throwing in nasty sql everywhere. Anyone around can give me a few pointers? Many thanks! -- Posted via http://www.ruby-forum.com/.
Matthew Margolis
2006-Jul-15 12:13 UTC
[Rails] Render -> ActionController -> Render -> ...
El Jeffo wrote:> Hi Guys, > > ActiveRecord is great for being able to drill down through many tables. > I often locate the very "top" record, and have a render :action => > "show" display that top record. > > Often I want to drill down like: > @kingdom.phylums.classes.orders.each do |order| > render :partial => "order/show" > end > > > But, depending on the user who is logged in, they see a different set of > "order", or a different set of "phylums". > > In other words, as I drill down, I want to filter what the result set > (has_many) by data not in the ActiveRecord. I want to filter by data > that is from a web form, from ajax, or from session variables. > > ActiveRecord makes it easy enough to do so with data in the record such > as: > has_many :phylums, :finder_sql => "blah blah blah where kingdom_id = > #{id} " > > But it can''t do something like > has_many :authorized_orders, :conditions => "order_owned_by = > session[:current_user]" > > > > I tried to get around this by thinking of: > render :partial => "call another action" > > But render to my knowledge can''t really go calling another > ActionController to render more data. > > So I''m kind of stuck... there''s no easy way to filter by external data, > and there''s no easy way to break out of an ActionView and include output > from an ActionController. > > I don''t want to go totally breaking this "MVC" idea by throwing in nasty > sql everywhere. > > Anyone around can give me a few pointers? Many thanks! > >Couldn''t you just go has_many :authorized_orders, :conditions => "order_owned_by = #{session[:current_user]}" where session[:current_user] is the id of your current user and get what you want? Matthew Margolis blog.mattmargolis.net
Your suggestion is good, and I tried that. But ActiveRecord doesn''t have a clue what the session[] or params[] variables are. Only ActiveController knows. I was just curious if ActiveRecord had any way of knowing about session[] params[] or even @some_other_var_in_the_controller_or_view so that a filter could be built on external variables. Matthew Margolis wrote:> El Jeffo wrote: >> >> has_many :phylums, :finder_sql => "blah blah blah where kingdom_id = >> >> Anyone around can give me a few pointers? Many thanks! >> >> > Couldn''t you just go > > has_many :authorized_orders, :conditions => "order_owned_by > #{session[:current_user]}" > > where session[:current_user] is the id of your current user and get what > you want? > > > Matthew Margolis > blog.mattmargolis.net-- Posted via http://www.ruby-forum.com/.