0

This is what's in my controller. I'm using devise and I'm trying to show take the user email and pass it into a where to find the specific family so I can display just the unique family controlled by the user.

I'm having an issue with the string interpolation. If I hard code the email into the query it works fine. But I want it to be dynamic. Thanks for the help!

home_controller.rb

  user_email = current_user.email
  @unique_family = Unit.where(:accountOwner => '#{user_email}')

3 Answers 3

2

For string interpolation you need to use double quotes "", like:

name = 'Some name'
puts "Hello #{name}"
# => "Hello Some name"

You see there name is defined within single quotes, but using the puts and interpolating the "Hello" string with the name variable, then is necessary to use double quotes.

To make your where query you can try with:

user_email = current_user.email
@unique_family = Unit.where(:accountOwner => user_email)

In such case isn't necessary to interpolate your user_email with "nothing" or trying to convert it to string, unless you want to make sure what's being passed will be an string, so you could do:

@unique_family = Unit.where(:accountOwner => user_email.to_s)

Or better:

@unique_family = Unit.where('accountOwner = ?', user_email)

That will bind the passed value for user_email to your query.

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

3 Comments

I'm getting <Unit::ActiveRecord_Relation:0x007fdce0ef4da8>.. My loop that is displaying this... ' <% @unique_family.each do |p1| %> <%= link_to "#{p1.familyname}", unit_path(p1)%><br> <% end %> <% else %> <h3 class="h2-white pull-left">No Families Yet</h3> <% end %>'
Is that the @unique_family?, so, do a variable inspect @unique_family.inspect, and if you want to print the values from the Relation the you can use each.
Thanks. In the end this is what worked. @unique_family = Unit.where("accountowner = ? ", email ) I think one of the issues was connected to accountOwner needing to be lowercased in my database.
0

I'd suggest you create association User <=> Unit, something like

in user.rb

has_one :family, class_name: Unit, foreign_key: :email

Comments

0

Just use the normal where method:

user_email = current_user.email
@unique_family = Unit.where(accountOwner: user_email)

Comments

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.