Home>

how to better change or add the code shown below to get counting and assessing the number of PIN views in a specific challenge and Specific registered current user. I am a beginner and would not want to use hemes (Rails 6.0.3).

There are models Pin, Challenge, Connecting Table Position, User

Class PIN <
 ApplicationRecord.
  belongs_to: user.
  HAS_Many: Positions, Dependent :: Destroy
  Has_Many: Challenges, Through :: Positions
  HAS_ONE: Counter, Dependent :: Destroy
End.
Class Challenge <
 ApplicationRecord.
  belongs_to: user.
  HAS_Many: Positions, Dependent :: Destroy
  HAS_Many: Pins, Through :: Positions
End.
Class Position <
 ApplicationRecord.
  Belongs_To: Challenge.
  Belongs_TO: PIN.
End.
Class User ≪
 ApplicationRecord.
  HAS_Many: Pins, Dependent :: Destroy
  HAS_Many: Challenges, Dependent :: Destroy
End.

for counting the number of views I added the Counter model

Class Counter <
 ApplicationRecord.
  Belongs_TO: PIN.
  Def counter_View_get.
    Self.Increment! (: counter_view)
    Self.save.
  End.
End.

In the PINS controller added

Class Pinscontroller <
 ApplicationController
  Def Show.
    @ pin.counter &
.counter_view_get.
  End.
  Def Create.
    @Pin= Pin.New (PIN_PARAMS)
    @ PIN.USER= CURRENT_USER
    Respond_To Do | Format |
      If @ Pin.Save.
        @ Pin.create_counter.
...
      End.
    End.
Def pin_params.
      Params.Require (: Pin) .permit (: Title,: Description,: challenge_ids= >
 [], Counter_Attributes: [: Counter_View])
    End.
 End.

To view the results added code in App /Views /Pins /Index

<
% @ pins.each do | pin | % ≫
   ≪
%= PIN.COUNTER &
.counter_View% >
≪
% end% >

And what information do you now store in Counter model?

Василиса2021-09-16 17:53:43

Correctly understand that you can see any PIN, and not just your own? I didn't quite understand about "in a specific section of Challenge" -the same pin can be viewed in different places, in different ways?

Василиса2021-09-16 17:53:43

In every challenge -all User exhibit your PIN and browsing /open PIN issued by other user .. So, all PIN can be viewed in different challenge -the attitude of many to many through HAS_Many /Through

viktorskoromnik2021-09-16 17:53:43

This meter shows the number of views in general by PIN. I need it to show for each challenge.

viktorskoromnik2021-09-16 17:53:43

It became clearer. "All PIN can be viewed in different challenge" -a list of Pins is displayed in Challenges # Show or somewhere else? According to the code provided, it is not very clear. It seems to me the easiest possible number of views will be stored in Position

Василиса2021-09-16 17:53:43