0

In html.erb I have:

<%= ContactDescribe.where(["contact_describe_id = ?", "12"]).limit(1).pluck(:borrower_or_lender_text)  %> 

The field is retrieved successfully. But returns an array element. I need to learn how to convert that element to a string.

emm
  • 19
  • 1
  • 6

3 Answers3

1

In addition to Deepak's answer, you can also convert the Array into a "Sentence" String

<%= ContactDescribe.where(contact_describe_id: 12).limit(1).pluck(:borrower_or_lender_text).to_sentence %>

Recommendation:

  • As pointed out by TheChamp, it is best practice to already "prepare" the values needed in the views as instance variables from the controller. See my recommended refactor

    # controller
    def YOUR_ACTION_NAME
      @contact_describe = ContactDescribe.where(contact_describe_id: 12).first
    end
    
    # view
    <%= @contact_describe.borrower_or_lender_text %>
    

Note: you won't even need pluck anymore unless you have other reasons why you want limit(1)

Jay-Ar Polidario
  • 6,463
  • 14
  • 28
  • Thanks for the suggestion. Okay this is new to me. In the the controller, show view I only define the parent record. Can I also define additional values like @contact_describe? – emm May 16 '17 at 13:44
  • @emm, yes "normally", feel free to add instance_variables as much as you want in the controller. The instance_variables normally are Record objects / any complicated stuff that needs to be put into a "variable" to be shown on the views. However, don't get too carried away and assign every possible code in the view as instance variables! :) (case in point: don' t do the following in the controller! `@contact_describe_borrower_or_lender_text = ContactDescribe.where(contact_describe_id: 12).first.borrower_or_lender_text` as you might need to reuse `@contact_describe` in the views down the road . – Jay-Ar Polidario May 16 '17 at 13:53
1

The issue here is that where returns a collection - something similar to an array, just in ActiveRecord - no matter what limit you set on it. To retrieve the information you would use .first or [0] since you always only return one object.

But, since you are looking for a specific ContactDescribe object. Do this instead:

ContactDescribe.find_by(contact_describe_id: 12).borrower_or_lender

Additionally there two things you should improve in your code.

1: Logic should go into the controller or the model. A view is solely here to show objects.

2: What is up with the contact_describe_id field? Why not call it id. It seems redundant. Isn't user.id more convenient than user.user_id?

davegson
  • 8,205
  • 4
  • 51
  • 71
-1

You can make use of join

<%= ContactDescribe.where(contact_describe_id: 12).limit(1).pluck(:borrower_or_lender_text).join(',')  %>
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
  • That's a different part – Deepak Mahakale May 16 '17 at 12:48
  • 3
    The main question is different and this answer solves the main question – Deepak Mahakale May 16 '17 at 12:49
  • @TheCha͢mp the policy of SO is [Be Nice](https://stackoverflow.com/help/be-nice). If you don't agree with this answer, *then downvote*, that's what they are for. There is no need for comments like that and it just wastes everyone's time. DeepakMahakale is under no obligation to help *anyone*. – CalvT May 16 '17 at 13:12
  • ye I guess I got too emotional..., sorry Deepak – davegson May 16 '17 at 13:14
  • Thanks. In a different context this suggestion may be useful. Or suggested join ridiculous, hence The Champ comments? – emm May 16 '17 at 14:35
  • yes in a different context `join()` is very useful, I overreacted because I felt he should address other issues with your code as well – davegson May 16 '17 at 14:54