accessing key/value pairs in views from model TYPES list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

accessing key/value pairs in views from model TYPES list

fugee ohu
I have a TYPES list in my model for ADDRESS_STATE_TYPES like ADDRESS_STATE_TYPES = 
  [  ['Alabama', 'AL'], ...
Now in my controller i have the value of address_state from the pararms list which  for the first in the list would be 'AL' How can I retrieve the 'Alabama' column in my controller if I have the other value, 'AL' from params list
Thanks in advance

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be118090-9621-464d-8a53-0122fc8e975c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: accessing key/value pairs in views from model TYPES list

Walter Lee Davis

> On Apr 25, 2017, at 8:09 AM, fugee ohu <[hidden email]> wrote:
>
> I have a TYPES list in my model for ADDRESS_STATE_TYPES like ADDRESS_STATE_TYPES =
>   [  ['Alabama', 'AL'], ...
> Now in my controller i have the value of address_state from the pararms list which  for the first in the list would be 'AL' How can I retrieve the 'Alabama' column in my controller if I have the other value, 'AL' from params list
> Thanks in advance

Well, since you have the data in an array, you can't index into it like you could in a Hash. There you could have

        { 'AL' => 'Alabama', ... }

and then you could find by either keys or values using
name = ADDRESS_STATE_TYPES['AL']
or
code = ADDRESS_STATE_TYPES.key('Alabama').

But in an array, you're going to have to iterate to find it:

def name_for_code(code)
  ADDRESS_STATE_TYPES.each do | pair |
    return pair.first if pair.last == code
  end
end
 
Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be118090-9621-464d-8a53-0122fc8e975c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/E0E0E247-BC1A-42CE-A703-7F5A1FA7BF8C%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: accessing key/value pairs in views from model TYPES list

Hassan Schroeder-2
On Tue, Apr 25, 2017 at 4:18 PM, Walter Lee Davis <[hidden email]> wrote:

> But in an array, you're going to have to iterate to find it:
>
> def name_for_code(code)
>   ADDRESS_STATE_TYPES.each do | pair |
>     return pair.first if pair.last == code
>   end
> end

or e.g.

2.3.3 (main):0 > types = [['Alabama','AL'],['California','CA']]
=> [
  [0] [
    [0] "Alabama",
    [1] "AL"
  ],
  [1] [
    [0] "California",
    [1] "CA"
  ]
]
2.3.3 (main):0 > types.to_h.invert.dig("CA")
=> "California"
2.3.3 (main):0 >

--
Hassan Schroeder ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yBz-wRPMY7HUx2Dw_9VqAb3vwjU-Pbn06FRtRf%3D2i5Ltw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: accessing key/value pairs in views from model TYPES list

fugee ohu


On Tuesday, April 25, 2017 at 7:27:25 PM UTC-4, Hassan Schroeder wrote:
On Tue, Apr 25, 2017 at 4:18 PM, Walter Lee Davis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="_NIY8YwyAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">wa...@...> wrote:

> But in an array, you're going to have to iterate to find it:
>
> def name_for_code(code)
>   ADDRESS_STATE_TYPES.each do | pair |
>     return pair.first if pair.last == code
>   end
> end

or e.g.

2.3.3 (main):0 > types = [['Alabama','AL'],['California','CA']]
=> [
  [0] [
    [0] "Alabama",
    [1] "AL"
  ],
  [1] [
    [0] "California",
    [1] "CA"
  ]
]
2.3.3 (main):0 > types.to_h.invert.dig("CA")
=> "California"
2.3.3 (main):0 >

--
Hassan Schroeder ------------------------ <a href="javascript:" target="_blank" gdf-obfuscated-mailto="_NIY8YwyAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">hassan.s...@...
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Thanks 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4272df64-fcf7-4abe-8037-7790777faa62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: accessing key/value pairs in views from model TYPES list

fugee ohu
In reply to this post by Walter Lee Davis


On Tuesday, April 25, 2017 at 7:18:40 PM UTC-4, Walter Lee Davis wrote:

> On Apr 25, 2017, at 8:09 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="IbUwgxIyAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I have a TYPES list in my model for ADDRESS_STATE_TYPES like ADDRESS_STATE_TYPES =
>   [  ['Alabama', 'AL'], ...
> Now in my controller i have the value of address_state from the pararms list which  for the first in the list would be 'AL' How can I retrieve the 'Alabama' column in my controller if I have the other value, 'AL' from params list
> Thanks in advance

Well, since you have the data in an array, you can't index into it like you could in a Hash. There you could have

        { 'AL' => 'Alabama', ... }

and then you could find by either keys or values using
name = ADDRESS_STATE_TYPES['AL']
or
code = ADDRESS_STATE_TYPES.key('Alabama').

But in an array, you're going to have to iterate to find it:

def name_for_code(code)
  ADDRESS_STATE_TYPES.each do | pair |
    return pair.first if pair.last == code
  end
end
 
Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="IbUwgxIyAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="IbUwgxIyAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/be118090-9621-464d-8a53-0122fc8e975c%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/be118090-9621-464d-8a53-0122fc8e975c%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/be118090-9621-464d-8a53-0122fc8e975c%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/be118090-9621-464d-8a53-0122fc8e975c%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


Thanks 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b6fb5717-7626-4b1d-89fe-e4d7c1bc8e52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...