putting options for select list in model

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

putting options for select list in model

fugee ohu
I've been putting options for select lists in my models but I haven't been able to do this with option/value pairs Anyone know how? The code below doesn't work

  DURATION_TYPES =  [['7 days', '7' ],['14 days', '14'], ['21 days','21'], ['30 days','30']]
  validates :duration, inclusion: { in: DURATION_TYPES }

--
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/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: putting options for select list in model

Walter Lee Davis

> On Apr 30, 2018, at 5:07 AM, fugee ohu <[hidden email]> wrote:
>
> I've been putting options for select lists in my models but I haven't been able to do this with option/value pairs Anyone know how? The code below doesn't work
>
>   DURATION_TYPES =  [['7 days', '7' ],['14 days', '14'], ['21 days','21'], ['30 days','30']]
>   validates :duration, inclusion: { in: DURATION_TYPES }
>
>

When this validator runs, what is it comparing? The label or the value? You are passing a combination of the two into your validator and expecting it to guess. You could try either using a hash for the data structure, and being explicit about what you want it to compare:

DURATION_TYPES = {'7 days' => 7, '14 days' => 14 ... }
validates :duration, inclusion: { in: DURATION_TYPES.values }

or you could extract just the second value out each pair (using your existing nested array):

validates :duration, inclusion: { in: DURATION_TYPES.map(&:last) }

Please google "Ruby symbol to Proc" to better understand how that last one works. If you use the first approach, then you will need to change the way you access the constant's values in your select list constructor.

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/c04d58fa-0e5b-42f8-8d80-20375f07f98c%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/75447E69-9828-4F9E-9E6E-A001CFE23FA3%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: putting options for select list in model

fugee ohu


On Monday, April 30, 2018 at 8:49:20 AM UTC-4, Walter Lee Davis wrote:

> On Apr 30, 2018, at 5:07 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Nn98hG6XBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I've been putting options for select lists in my models but I haven't been able to do this with option/value pairs Anyone know how? The code below doesn't work
>
>   DURATION_TYPES =  [['7 days', '7' ],['14 days', '14'], ['21 days','21'], ['30 days','30']]
>   validates :duration, inclusion: { in: DURATION_TYPES }
>
>

When this validator runs, what is it comparing? The label or the value? You are passing a combination of the two into your validator and expecting it to guess. You could try either using a hash for the data structure, and being explicit about what you want it to compare:

DURATION_TYPES = {'7 days' => 7, '14 days' => 14 ... }
validates :duration, inclusion: { in: DURATION_TYPES.values }

or you could extract just the second value out each pair (using your existing nested array):

validates :duration, inclusion: { in: DURATION_TYPES.map(&:last) }

Please google "Ruby symbol to Proc" to better understand how that last one works. If you use the first approach, then you will need to change the way you access the constant's values in your select list constructor. 

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="Nn98hG6XBQAJ" 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="Nn98hG6XBQAJ" 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/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c04d58fa-0e5b-42f8-8d80-20375f07f98c%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.

Before being able to continue with option/value pairs I actually can't get this working in a simpler way just the options and values being the same, I still get 'not included in the list' error In my schema, duration is a string column

  DURATION_TYPES = ['7 days', '14 days'] 
  validates :duration, inclusion: DURATION_TYPES

--
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/6533f9ee-3031-4936-bf09-aeb58773e12b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: putting options for select list in model

fugee ohu
In reply to this post by Walter Lee Davis


On Monday, April 30, 2018 at 8:49:20 AM UTC-4, Walter Lee Davis wrote:

> On Apr 30, 2018, at 5:07 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Nn98hG6XBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I've been putting options for select lists in my models but I haven't been able to do this with option/value pairs Anyone know how? The code below doesn't work
>
>   DURATION_TYPES =  [['7 days', '7' ],['14 days', '14'], ['21 days','21'], ['30 days','30']]
>   validates :duration, inclusion: { in: DURATION_TYPES }
>
>

When this validator runs, what is it comparing? The label or the value? You are passing a combination of the two into your validator and expecting it to guess. You could try either using a hash for the data structure, and being explicit about what you want it to compare:

DURATION_TYPES = {'7 days' => 7, '14 days' => 14 ... }
validates :duration, inclusion: { in: DURATION_TYPES.values }

or you could extract just the second value out each pair (using your existing nested array):

validates :duration, inclusion: { in: DURATION_TYPES.map(&:last) }

Please google "Ruby symbol to Proc" to better understand how that last one works. If you use the first approach, then you will need to change the way you access the constant's values in your select list constructor.

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="Nn98hG6XBQAJ" 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="Nn98hG6XBQAJ" 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/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c04d58fa-0e5b-42f8-8d80-20375f07f98c%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c04d58fa-0e5b-42f8-8d80-20375f07f98c%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.


This simpler use causes the same 'not included in the list' error

  DURATION_TYPES = ['7 days', '14 days'] 
  validates :duration, inclusion: DURATION_TYPES
 

--
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/5697b649-8868-4eac-aa8d-2df330a23770%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.