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 9237445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:32:12+00:00 2026-06-18T07:32:12+00:00

I have two models like that class Plan < ActiveRecord::Base belongs_to :profile And class

  • 0

I have two models like that

class Plan < ActiveRecord::Base
    belongs_to :profile

And

class Profile < ActiveRecord::Base
    has_many :plans

And routes like: (I need to)

resources :profiles do
    resources :plans
end
resources :plans

So, following up ruby-on-rails – Problem with Nested Resources, I’ve made my PLANS index controller like this, to works NESTED and UNESTED at same time (the only way I’ve found for now):

def index
  if params.has_key? :profile_id
    @profile = Profile.find(params[:profile_id])
    @plans = @profile.plans
  else
    @plans = Plan.all
end

There is a cleaner approach to this?

I have another models in this situation, and putting all actions, in all controllers to behave like this is cumbersome.

  • 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-06-18T07:32:13+00:00Added an answer on June 18, 2026 at 7:32 am

    You gave me an idea:

    models/user.rb:

    class User < ActiveRecord::Base
      has_many :posts
      attr_accessible :name
    end
    

    models/post.rb:

    class Post < ActiveRecord::Base
      belongs_to :user
      attr_accessible :title, :user_id
    end
    

    controllers/posts_controller.rb:

    class PostsController < ApplicationController
      belongs_to :user # creates belongs_to_user filter
    
      # @posts = Post.all # managed by belongs_to_user filter
    
      # GET /posts
      # GET /posts.json
      def index
        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @posts }
        end
      end
    end
    

    And now the substance:

    controllers/application_controller.rb:

    class ApplicationController < ActionController::Base
      protect_from_forgery
    
      def self.belongs_to(model)
                                                          # Example: model == :user
        filter_method_name = :"belongs_to_#{model}_index" # :belongs_to_user_index
        foreign_key        = "#{model}_id"                # 'user_id'
        model_class        = model.to_s.classify          # User
    
        class_eval <<-EOV, __FILE__, __LINE__ + 1
          def #{filter_method_name}                                                # def belongs_to_user_index
            if params.has_key? :'#{foreign_key}'                                   #   if params.has_key? :user_id
              instance_variable_set :"@#{model}",                                  #     instance_variable_set :"@user",
                                    #{model_class}.find(params[:'#{foreign_key}']) #                           User.find(params[:user_id])
              instance_variable_set :"@\#{controller_name}",                       #     instance_variable_set :"@#{controller_name}",
                                    @#{model}.send(controller_name.pluralize)      #                           @user.send(controller_name.pluralize)
            else                                                                   #   else
              instance_variable_set :"@\#{controller_name}",                       #     instance_variable_set :"@#{controller_name}",
                                    controller_name.classify.constantize.all       #                           controller_name.classify.constantize.all
            end                                                                    #   end
          end                                                                      # end
        EOV
    
        before_filter filter_method_name, only: :index # before_filter :belongs_to_user_index, only: :index
      end
    end
    

    The code is not complex to understand if you have notions of Ruby metaprogramming: it declares a before_filter which declares the instance variables inferring the names from the controller name and from the association. It is implemented just for the index actions, which is the only action using the plural instance variable version, but it should be easy to write a filter version for the other actions.

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

Sidebar

Related Questions

Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I have a two models set up like this: class User < ActiveRecord::Base #
I have two Models here associated as such: class Order < ActiveRecord::Base belongs_to :customer
I have two models class Project < ActiveRecord::Base attr_accessible :created_at, :name, :status, :company_id has_many
I have two models: class Calendar < ActiveRecord::Base has_many :events end class Event <
I have two models: Company and Person class Person < ActiveRecord::Base belongs_to :company end
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
I have two object models that looks like this: public class MyObject { public
I have two models: class Wine belongs_to :region end class Region has_many :wines end
I have two models: class Sentence < ActiveRecord::Base attr_accessible :sentence_id, :authority_name #... end class

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.