Create a select with HABM

14 views
Skip to first unread message

Felipe Pieretti Umpierre

unread,
Mar 5, 2012, 4:00:17 PM3/5/12
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Mar 5, 2012, 4:04:28 PM3/5/12
to rubyonra...@googlegroups.com
On 5 March 2012 21:00, Felipe Pieretti Umpierre <li...@ruby-forum.com> wrote:
> 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.

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

Colin Law

unread,
Mar 5, 2012, 4:07:14 PM3/5/12
to rubyonra...@googlegroups.com

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

Colin

Felipe Pieretti Umpierre

unread,
Mar 5, 2012, 4:17:37 PM3/5/12
to rubyonra...@googlegroups.com
>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.

Colin Law

unread,
Mar 5, 2012, 4:31:23 PM3/5/12
to rubyonra...@googlegroups.com
On 5 March 2012 21:17, Felipe Pieretti Umpierre <li...@ruby-forum.com> wrote:
>>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

Colin Law

unread,
Mar 5, 2012, 4:36:03 PM3/5/12
to rubyonra...@googlegroups.com
On 5 March 2012 21:00, Felipe Pieretti Umpierre <li...@ruby-forum.com> wrote:
> 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.

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

Felipe Pieretti Umpierre

unread,
Mar 5, 2012, 6:29:11 PM3/5/12
to rubyonra...@googlegroups.com
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

Eddie Lau (http://36613.me)

unread,
Mar 5, 2012, 7:38:01 PM3/5/12
to rubyonra...@googlegroups.com, rubyonra...@googlegroups.com
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

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>

Colin Law

unread,
Mar 6, 2012, 3:31:11 AM3/6/12
to rubyonra...@googlegroups.com
On 5 March 2012 23:29, Felipe Pieretti Umpierre <li...@ruby-forum.com> wrote:

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

Reply all
Reply to author
Forward
0 new messages