0

I'm pretty new to Rails and wanted to do something along the lines of choosing a subset of objects in my Model. For example, I have a Project model and wanted to select some subset of projects based on some join table with another model, Organizations.

My initial thought was to create some helper method in projects_helper.rb that would perform the appropriate lookup on to determine which projects to return.

Another thought was to utilize scoping as described here (http://apidock.com/rails/ActiveRecord/Scoping/Named/ClassMethods/scope).

Both seem to functionally complete the objective, but what would be the best practice way of accomplishing this? Is there a key difference as to what can access each of these approaches?

Thanks!

kgong
  • 145
  • 1
  • 7
  • Have a look at the answer here: http://stackoverflow.com/questions/5899765/activerecord-rails-3-scope-vs-class-method – vee Aug 13 '13 at 02:46
  • Thanks! Guess this didn't show up since I used helper method in my query as opposed to class method... – kgong Aug 13 '13 at 03:59

1 Answers1

0

Depends on the point of view of the question being "asked" by the query.

If you are asking for an Organization's projects, you might choose the Org first, then display organization.projects. Nothing fancy going on there beyond linking the models appropriately (organization has_many projects, project belongs_to organization) and having the organization_id as a foreign key in the projects table.

I'm not sure if a named scope would be appropriate if the Organizations in your system are a dynamic quantity. You don't want to have a named scope for each if the list of organizations changes all that often.

railsdog
  • 1,503
  • 10
  • 10
  • Thanks. Additional relevant information is here: http://stackoverflow.com/questions/5899765/activerecord-rails-3-scope-vs-class-method, as shared by vinodadhikary. – kgong Aug 13 '13 at 04:01