Create a select with HABM

Hello, I have two tables with association HABM, projects and users, and in projects I want to show all users that was associated to the project.

I have try with this:

<%= select( "user", "id", Project.all.collect { |p| [ p.name, p.id ] } ) %>

But only show all my projects, but Iz don't know who show only the user in a determinated project.

class Project < ActiveRecord::Base

  has_attached_file :image, :styles => { :medium => "300x200>", :thumb => "100x100>" }

  has_many :item   has_and_belongs_to_many :users   belongs_to :category

  belongs_to :color   belongs_to :icon

  # kaminari   # https://github.com/amatsuda/kaminari   paginates_per 6

end

class User < ActiveRecord::Base

  has_and_belongs_to_many :projects

end

Thank you for the help.

If you have a project in @project for example, then to get all the associated users you just use @project.users That is the magic of Rails.

I suggest that you work right through a good tutorial such as railstutorial.org (which is free to use online), then you will get a grasp of the basics of Rails.

Colin

Also have a look at the Rails Guide on Associations, and all the others in fact.

Colin

Colin,

@project.users That is the magic of Rails.

This magic I already know, but my question was, I want to show All user associated in a project in a select tag.

But I don't know if I make a each in @project.users and do a each or has something already done with rails.

Sorry I misread what you wanted, I will replied again to the original mail

Colin

The code you have will show all projects because you have specified Project.all. I don't understand exactly what you want to show. Do you want the users to appear in a select box or what? Do you have a particular project? Is it in a variable? Please be a bit more specific about what you want to see on the page.

Colin

Hello Colin, thank you for Ur attention.

I have a model Project, in my projects I can create items, in this project I can Select various user, that will be available in my items to distribute that users that I select to make part of my project.

But when I try to make a select of all users that are in this project, I can't because I don't know very well rails yet, and don't know if have something already done or I will have to improvise.

Thank you again

Do u mean double select ? You have two select drop down, and u want the second one contains only the users which belongs to the selected project in the first select drop down ?

3dd13 developer http://3dd13.me

Please don't top post, it makes it difficult to follow the thread. Insert your comments at appropriate points in previous message. Thanks

Also by completely removing the previous message here you have lost all context so nobody finding this in the future will know what the post is about.

Hello Colin, thank you for Ur attention.

I have a model Project, in my projects I can create items, in this project I can Select various user, that will be available in my items to distribute that users that I select to make part of my project.

But when I try to make a select of all users that are in this project, I can't because I don't know very well rails yet, and don't know if have something already done or I will have to improvise.

Previously you showed a select that used Project.all to get the projects to display. Now you say you have a particular project (let us assume, since though I asked you did not answer the question) that it is in @project. Then all you have to do is use a select similar to the one you already had, but use @project.users instead of Project.all. Obviously you have to change the contents of the collect block also as it will be receive users instead of projects.

How are you getting on with working through railstutorial.org? You should be well on the the way through it by now. If you had completed it you would not have needed to ask this question.

Colin