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% >
- rails6 reset css etc. are read twice
- Server startup failed with Ruby on Rails
- CANCANCAN GEM does not work in CONTROLLER and ABILLITY targets in different cases such as DashboardController and become Uniniti
- Rails About extraction after combining three tables
- Counter sending messages
- I want to make a good function but I do not see the foreign key and undefined method `id 'for nil: nilclass
- Press the Like button to fly to the Post Detail page
- LINE login can not be made with RAILS5
- If you try to deploy the Rails post app through Unicorn, Bundler: Failed to Load Command: Unicorn_rails
- About the behavior of URL after validation error of NEW action and error associated with it