Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:43:38+00:00 2026-05-23T09:43:38+00:00

I am working on a Rails 2.3.9 app and my question involves both a

  • 0

I am working on a Rails 2.3.9 app and my question involves both a self referencial relationship and a named_scope. This application allows users to log and share workouts. A workout can be public or private and that is designated by @workout.public == 1.

I allow users to ‘follow’ people. So on a current_user’s dashboard I display all public workouts from users that current_user follows with the following code:

/dashboard/index.html.erb

 <% current_user.friends_workouts.each do |workout| %>
  <%= link_to (workout.title), workout %> <br/>
  by <%= link_to (workout.user.username), workout.user %> - <%= time_ago_in_words(workout.created_at)%> ago</p>
 <% end %>

user.rb

def friends_workouts
 @friends_workouts ||= Workout.current.public_workouts.find_all_by_user_id(self.friends.map(&:id), :order =>  "created_at DESC", :limit => 3)
end

workout.rb

named_scope :public_workouts, :conditions => {:public => 1 }, :order => "created_at DESC"

I now want to add a condition to this scope as I am adding another level of sharing. Users can associate to a “box” (a gym really) through a “membership” model. So if the current_user belongs_to the same “box” as a user they follow, they should not only see the workouts marked public but also workouts where @workout.box_only == 1.

How can I affect the above to include all public workouts from followed users AND workouts from followed users where @workout.box_only == 1 and @workout.user.membership.box_id == current_user.membership.box_id. I know that syntax is incorrect but you get my point (hopefully).

UPDATE:

It also needs to be considered that :public_workouts is being called from pages that don’t require a logged_in? user so in that case if the scope is trying to reference current_user it will throw an error.

UPDATE 2:
:user has_many :memberships

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:43:39+00:00Added an answer on May 23, 2026 at 9:43 am

    I believe something like the following should do it for you:

    named_scope :public_workouts, 
                :joins => ", user, membership"
                :conditions => 
                  "workouts.public = 1 or
                   membership.box_id = #{current_user.membership.box_id}",
                :group => "workouts.id",
                :order => "workouts.created_at DESC"
    

    You would have to play around with this for a bit. The hard part every time I try something like this is to get the OR conditions correct. You want to get all public and those where the joined membership.box_id matches regardless of public being 1.

    Edit: Admittedly this is perhaps not the most ruby way of building a query and I haven’t tested it properly but something like below could also be made to work.

    def self.public_workouts
      query = Workout.joins(:user => { :membership })
      if current_user
        query.where('memberships.box_id = ? or workouts.public = 1', current_user.membership.box_id) unless current_user.membership.box_id.nil?
      else
        query.where('workouts.public = 1')
      end
      query.group('workouts.id')
      query.order("workouts.created_at DESC")
      return query
    end
    

    Edit2
    Another alternative could be to create two separate scopes and create a class method that combines the two scopes. This class method would then be used in the view.

    named_scope :box_workouts, 
                :joins => ", user, membership"
                :conditions => "memberships.box_id = #{current_user.membership.box_id}"
                :group => "workouts.id",
                :order => "workouts.created_at DESC",
                :select "workouts"
    
    named_scope :public_workouts, 
                :conditions => :public => 1
                :order => "workouts.created_at DESC"    
    
    def self.public_box_workouts
      return box_workouts.merge(public_workouts).limit(3) if current_user
      return public_workouts.limit(3)
    end     
    

    Edit3 Not so hard, I believe something like below will work.

    def self.box_and_public_workouts(user)
      return public_workouts if user.nil? or user.memberships.blank?
      return public_workouts + box_workouts(user.memberships.map(&:box_id))
    end
    
    named_scope :box_workouts, lambda { |box_ids| { :conditions => ['box_id IN (?)', box_ids], :order => 'created_at DESC' } }
    

    Apologies for taking so long. I was missing confused with how the “old” way of querying the database. I went right for Rails3 🙂

    Anyway, I didn’t want to commit anything so I tried to fork it to send a pull request but github is being rude to me tonight. Might just copy from here then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Rails app that existing users can invite additional members to
I am working on a Rails app that allows you to create a configuration
I am working on a rails app to self teach BDD and testing in
I'm a new Rails programmer working on a web app. As part of this
I'm working on a Rails app and am looking to include some functionality from
I'm working on a rails app that has a whole bunch of before filters
I'm working on a Rails app that sends data through a form. I want
I'm working on a Rails app that will contain information on a bunch of
So I'm working on a Rails app to get the feeling for the whole
I've been working on a rails app that uses the restful authentication plugin. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.