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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:53:26+00:00 2026-06-05T23:53:26+00:00

I am trying to get data for my collection_select list. Details There are 4

  • 0

I am trying to get data for my collection_select list.

Details

There are 4 tables involved

       volunteers 
            has_many :signed_posts
            has_many :posts, :through=>:signed_posts

       signed_posts
            belongs_to :volunteer
            belongs_to :post

       posts
            belongs_to :organization
            has_many :signed_posts
            has_many :volunteers, :through=>:signed_posts

       organizations
            has_many :post

If I would have to write in plain SQL, it would be like below.This sql is for postgreSQL. I would like to write in rails 3.2.1 syntax.

      Select sp.id,p.title ||'-'|| o.name AS project from signed_posts sp 
           inner join volunteers v on v.id=sp.volunteer_id
           inner join posts p on p.id=sp.post_id
           inner join organizations o on o.id=p.organization_id
           where v.id=1

As a result, I want to get signed_posts.id and posts.title – organizations.name for a volunteer id 1

which I would like to use on dropdown list.

Thank you very much for your help

My Solution

I solved the problem using hash table like this one

        @signed_projects=Volunteer.find(1).signed_posts.joins(:post)

        @ddl_test=Hash.new
            @signed_projects.each do |signed_project|
                   ddl_test[signed_project.post.title+"-"+signed_project.post.organization.name]=signed_project.id

           end

On View

                <%=select_tag 'ddl_test',options_for_select(@ddl_test.to_a),:prompt => 'Please select the project'%>

I am not completely satisfied. I think there must be some elegant solution. If any body has better idea, please let me know

  • 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-05T23:53:27+00:00Added an answer on June 5, 2026 at 11:53 pm

    There are a couple ways to do this. In your controller you would want a statement such as:

    @signed_posts = Volunteer.find(1).signed_posts.includes({:post => :organization})
    

    After that, one way would be to create a method in your SignedPost model to return the data you need, something like the following

    def post_name_with_organization
      "#{id} #{post.title} - #{post.organization.name}"
    end
    

    Then, in your collection_select, you can use (this is partially guessing, since I don’t know the specifics of your form):

    form.collection_select(:signed_post_id, @signed_posts, :id, :post_name_with_organization)
    

    Edit based on question updates

    If you want to use select_tag and options_for_select as you did in your solution, a more elegant way would be to create this variable in your controller:

    @signed_posts_options = @signed_posts.map {|sp| [sp.post_name_with_organization, sp.id]}
    

    Then in your view:

    <%=select_tag 'ddl_test',options_for_select(@signed_posts_options),:prompt => 'Please select the project'%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to get data out of a plist. It basically looks
I am trying to get data back from a PHP file assigned with GET
I am trying to get data from my interface, written in c, to another
I am working on an app on Windows Phone and trying to get data
I have a web service and I'm trying to get data by JSON. if
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
I am trying to get my data which is hierarchically set up with a
i am trying to get some data, but i dont know how can i
I am trying to get some data relevant to a stored procedure (or function)
I'm trying to get some data from a table, where the tag has no

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.