Ruby on Rails Wednesday, February 7, 2018

> On Feb 7, 2018, at 1:43 PM, fugee ohu <fugee279@gmail.com> wrote:
>
>
>
> On Wednesday, February 7, 2018 at 1:01:02 PM UTC-5, Colin Law wrote:
> On 7 February 2018 at 17:51, fugee ohu <fuge...@gmail.com> wrote:
>
>
> On Wednesday, February 7, 2018 at 12:38:57 PM UTC-5, Colin Law wrote:
> On 7 February 2018 at 17:19, fugee ohu <fuge...@gmail.com> wrote:
>
> I dunno why this doesn't produce any output
>
> <% @pictures=Picture.all %>
>
> Model.all has been deprecated since rails 2.3.8. Don't use it.
>
> <% @pictures.all do |picture| %>
>
> this should be using each, not all
>
> Colin
>
> <% if @person %>
> <% if picture.person_id == @person.id %>
> <%= check_box_tag 'person[picture_ids][]', picture.id, true %>
> <% else %>
> <%= check_box_tag 'person[picture_ids][]', picture.id %>
> <% end %>
> <%= image_tag picture.name.thumb %>
> <% end %>
> <% end %>
>
>
>
>
> I can use <% if picture.person_id == @person.id %> ? This is a has_many_through relationship Person has_many :pictures, through: :person_pictures and also Picture has_many :people, through: :person_pictures What's the right syntax to test if the picture is already associated to the person when iterating through pictures Also if I can't use Model.all in a routine I can still use it to get @pictures=Picture.all right?
>
> Model.all is deprecated, don't use it.
>
> If Picture has_many people then you can't use picture.person_id as one picture is associated with many people. so @picture.people is (effectively) an array of people. You will have determine whether the array includes that person.
>
> Colin
>
>
>
> --
> 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 rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8d160a04-6951-4159-bfa7-8eee046cb3c4%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> Thanks, yea that's what I meant How do I test for the inclusion of picture in @person.pictures If I can't use Model.all how do I select all pictures from the pictures table?

I would start by setting up @pictures in the controller, not the view.

@pictures = Picture.order :name

Then in the view, you could use that to create your field of checkboxes:

<%- @pictures.each do |picture| %>

Use the presence of that picture's ID in the @person's picture_ids array to set or not set the checked attribute
<%= check_box_tag 'person[picture_ids][]', picture.id, @person.picture_ids.include?(picture.id) %>
<%= do_something_to_show_a_thumbnail_here %>

<%- end %>

What you will end up doing is mutating the picture_ids array in this form. As long as you have allowed that (and defined it as requiring an array) in your whitelist, then the association will have built the necessary accessor methods and this will all just work.

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 rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/45dcd144-63d7-47c8-b92f-e842dd4d0bdf%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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1B8C2A4C-FF54-4774-9FDE-631BB8A63A52%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment