Question

I hope to get some great help here. I am a noob in many areas of Rails 3.2 but am getting much better.

I have a collection select that has retrieved the correct records. I have multiple properties, that then has multiple users. The collection select correctly uses a UserProperty table to filter only the property users (I want the collect to display each user related to that property). So I have the user ID and not the name.

So I tried to create an object by querying the parent table (users) to pull the parents properties and pass through. Unfortunately, the second query is only passing a single record. So scrapped that.

The easiest would be to use the 1st query in the collection_select but then display the parent field based on the reference user id. Is there a syntax that allow me to display text back to the parent (USER) table within the collection select and queried object "pool"?

The goal would be to query UserProperties by the nested route(@property) - Working. (@pool contains the correct results)

Then display the the name field from User. (User.name)

Here is my code:

Controller:

      def find_property

        @property = Property.find(params[:property_id])

        @pool = UserProperty.find_all_by_property_id(@property) 

      end

View:

    <%= fields_for (:assigned_to_user_id) do |pool| %>
        <%= pool.collection_select(:assigned_to_user_id, @pool, :id, :id  ) %>
        <!-- Need to change last field from id to name field of Users Table -->

Model Design: (need name from Users)

    # Table name: users
    #
    #  id              :integer          not null, primary key
    #  name            :string(255)
    #  email           :string(255)

    class User < ActiveRecord::Base
      attr_accessible :name, :email

      has_many :properties, through: :user_properties
    end

    # Table name: user_properties
    #
    #  id          :integer          not null, primary key
    #  user_id     :integer
    #  property_id :integer

    class UserProperty < ActiveRecord::Base
      attr_accessible :property_id, :user_id

      belongs_to :user
      belongs_to :property
    end
Was it helpful?

Solution

Issue Solved. Rails has built in filters based on nested routing. So a friend helped me solve this issue using a simpler method and along RoyTheBoy's suggestion.

Although there was a many through relation, there was a need just for a has many statement.

Models as follows:

    has_many :users, through: :user_properties
    has_many :user_properties  (new line)

Then remove the filter from the @pool

     @pool = @property.users.all  (rails automagically pulls from the nested route)

FYI - existing before_filter for loading nested properties

    @property = Property.find(params[:property_id])

This allowed a simpler collection_select

    <%= fields_for (:assigned_to_user_id) do |pool| %>
    <%= collection_select(:assigned_to_user_id, :user_id, @pool , :id, :name, prompt: true) %>

Nested routing for the property, handles the filtering automagically! Gotta love rails, especially a newbie like me! Thanks to Robert for the Help!

OTHER TIPS

from your table definitions, in your models you need

class UserPropery < ActiveRecord::Base
belongs_to :user

class User < ActiveRecord::Base
has_many :user_properties

you can then use

UserProperty.where(...).user.name

however you say "I have multiple properties, that then has multiple users."

this implies to me that a property can have many users, In this case you should have a user_propery_id field in your User table and in you models

class UserPropery < ActiveRecord::Base
has_many :user

class User < ActiveRecord::Base
belongs_to :user_properties

you can then use something like

UserProperty.where(...).users.collect {|user| [ user.id, user.name ]} 

this is a good explanation of how the associations work http://guides.rubyonrails.org/association_basics.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top