0

So tasking.point_of_contact is an integer value that is the same number as the user.id whose name (name is defined as :firstname + :lastname) I want to display.

How can I take the value in tasking.point_of_contact and use it to call the name of the corresponding user.id?

Right now I have:

<td><%= tasking.point_of_contact %></td>

I want that to display the name of the user whose user.id equals tasking.point_of_contact.

The relationship between user and taskings is a many-to-many.

I hope I've give you enough information. I may have been redundant, but I just want to be clear.

6
  • 1
    Please post the source of tasking and point_of_contact (user) active_record models Commented May 1, 2013 at 17:44
  • When formatting code, please use backticks (`) and not bold (**). See the help section for more details. Commented May 1, 2013 at 17:48
  • Cool, I'll format it with backticks next times, I'm still new to this! Thank you! Commented May 1, 2013 at 19:06
  • Here is my Tasking model: ` class Tasking < ActiveRecord::Base belongs_to :user, :foreign_key => "point_of_contact" has_many :userhastasks, :dependent => :destroy attr_accessible :description, :detail_date, :location, :point_of_contact, :task_name, :completed end ` Commented May 1, 2013 at 19:34
  • Here is my User model: ` class User < ActiveRecord::Base has_many :userhastasks, :dependent => :destroy devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :company, :classyear, :role def name lastname + ", " + firstname + " " + company end end ` Commented May 1, 2013 at 19:34

2 Answers 2

3

If you haven't set up your relationship in your tasking model yet, you want to do that.

belongs_to :user, :class_name => "User", :foreign_key => "point_of_contact"

Then, where ever you're doing the initial query

@tasking = Tasking.where(whatever)

You'll want to eager load the users so you don't get a N+1 issue (you can google this, just an efficiency thing).

@tasking = Tasking.includes(:user).where(whatever you want to find)

Then in your view you'll do

tasking.user.name
Sign up to request clarification or add additional context in comments.

4 Comments

Specifying the class User for a relationship already called user should be redundant.
true, didn't really think about that
Hey so i'm confused where @tasking = Tasking.includes(:user).where(whatever you want to find) is supposed to go. In my controller, model, or view? I've updated the tasking model as you've suggested. And I'm unclear as to what goes in (whatever you want to find)? Do you mean something like .where( user.id = tasking.point_of_contact )? Appreciate the help, still very new at RoR!
@dylan Where are you getting the tasking variable from now? That has to come from somewhere, right? The @tasking = Tasking.includes(:user).where(arbitrary) goes in the controller, in the action for whatever view you want. Then in that view, you can use the @tasking variable. If you want all tasks, you can do @tasking = Tasking.includes(:user).all. The .where is just a way for you to filter which Tasking items you want. You could say .where(:active => true), if you had an active column that was boolean. That kind of thing.
0

There are some helper methods generated automatically when you have a has_and_belongs_to_many relationship. Specifically, the others generated method should be helpful. In your case, it would be tasking.users. However, I'm not sure your design is correct...

3 Comments

Please don't use has_and_belongs_to_many. It's a relic from Rails 1 that is a lot more difficult to use than has_many x, :through y type relationships.
I will note that in no place in the source is has_and_belongs_to_many marked as deprecated.
I didn't say deprecated, it's still fully supported, but that doesn't mean it's better. It's absolutely not. Any metadata stored in that relationship is very difficult to edit, and will be joined in to the model it's associated with. The :through mechanism is much more robust, giving you a first-class model you can use to edit any metadata, and in many cases you'll find you'll be working with those join records directly, as when presenting a list of check-boxes on a form where relationships can be added or removed as required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.