Polymorphic Associations Status in concern - ruby-on-rails

I created concern in which placed association.
I need to make polymorphic association status.
has_one :status, class_name: 'VideoStatus', inverse_of: :video, dependent: :destroy
belongs_to :video, inverse_of: :status
I can't make that Association.How to make it?
require 'active_support/concern'
module EncodeStatuses
extend ActiveSupport::Concern
FORMATS = %w[mp4]
HIGH_VERSION = 'high'
MEDIUM_VERSION = 'medium'
LOW_VERSION = 'low'
VERSIONS = [HIGH_VERSION, MEDIUM_VERSION, LOW_VERSION]
included do
has_one :status, class_name: 'VideoStatus', inverse_of: :video, dependent: :destroy
accepts_nested_attributes_for :status, update_only: true
delegate :success?, :failure?, :waiting?, :encoding?, to: :status
end
end
models/video.rb
class Video < ActiveRecord::Base
include EncodeStatuses
...
end
models/post.rb
class Post < ActiveRecord::Base
include EncodeStatuses
...
end
models/video_status.rb
class VideoStatus < ActiveRecord::Base
STATUS_WAITING = 'waiting'
STATUS_ENCODING = 'encoding'
STATUS_SUCCESS = 'success'
STATUS_FAILURE = 'failure'
STATUSES = [STATUS_WAITING, STATUS_ENCODING, STATUS_SUCCESS, STATUS_FAILURE]
belongs_to :video, inverse_of: :status
belongs_to :post, inverse_of: :status
...
end

has_one :status, as: :video_status, dependent: :destroy
class VideoStatus < ActiveRecord::Base
belongs_to :video_status, polymorphic: true
end

Related

Building active record query

I'm trying to build a query such that if a driver sends an offer to a ride, that ride no longer shows up in the collection rendered in the index view.
I've tried so many variations and I'm still getting all the rides. Even the ones Ive sent an offer to.
lang - ruby
class Driver < ApplicationRecord
belongs_to :user
has_many :offers
has_many :rides
end
class User < ApplicationRecord
has_many :rides
has_one :driver
end
class Ride < ApplicationRecord
validates :to, presence: true
validates :from, presence: true
validates :directions_from, presence: true
has_many :offers
belongs_to :user
belongs_to :driver, optional: true
end
lang - ruby
class Driver < ApplicationRecord
belongs_to :user
has_many :offers
has_many :rides
end
class User < ApplicationRecord
has_many :rides
has_one :driver
end
class Ride < ApplicationRecord
validates :to, presence: true
validates :from, presence: true
validates :directions_from, presence: true
has_many :offers
belongs_to :user
belongs_to :driver, optional: true
end
class Offer < ApplicationRecord
belongs_to :ride
belongs_to :driver
end
def index
#location = current_user.currently_at
#rides = Ride.includes(:driver).where(from: #location).select do |ride|
ride.offers.map do |offer|
offer.driver.user != current_user
end
end
#offer = Offer.new
end

Rails 5 Active Model Serializer display has many conditions in JSON

In my project I have those models and relationships:
class Course < ApplicationRecord
has_many :segments, inverse_of: :course, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :segments, :allow_destroy => true
end
class Segment < ApplicationRecord
validates :data ,presence: true, if: :segment_is_video?
validates :segment_type ,presence: true
validates_presence_of :course
belongs_to :course, inverse_of: :segments
has_many :questions
accepts_nested_attributes_for :questions, :allow_destroy => true
def segment_is_video?
segment_type == 'Video'
end
end
class Question < ApplicationRecord
validates_presence_of :segment
belongs_to :segment, inverse_of: :questions
end
I want to display the data field only if the type is Video, and I want to display questions array only if type field is Quiz. I'm using Active Model Serializer for that but it's not working. the data still displaying when the type is Quiz and the question array doesn't show at all.
this is my code for the serializers:
class CourseSerializer < ActiveModel::Serializer
attributes :id, :title, :author
has_many :segments
def root
'Course'
end
end
class SegmentSerializer < ActiveModel::Serializer
attributes :id, :unit_id, :unit_title, :name, :segment_type, :data
belongs_to :course
has_many :questions, if: -> { isQuiz }
def root
'Segments'
end
def include_data?
object.segment_type == 'Video'
end
def isQuiz
object.segment_type == 'Quiz'
end
end
class QuestionSerializer < ActiveModel::Serializer
attributes :id, :question, :answer1, :answer2, :answer3, :answer4, :correct
belongs_to :segment
def root
'Question'
end
end

How to call an attribute from a class array

I'm trying to make a method that gets the date and adds onto it an amount of days specified.
At present I cannot call the days specified.
I have a Plant Class that has_many DaysTillSellables, The Period class has_many DaysTillSellable also.
When the user creates a plant they can add a DaysTillSellables and then select a period and then enter an amount of days.
I first need to check to see which period the date is in, then return that period. Currently attempting like so
def current_period
return unless completed_at_week.between?(period.start_week, period.finish_week)
index_days_till_sellables_on_period_id
end
Then Find the days till sellable that is connected to that period and finally call the days from that
Below is the code for the class I'm trying to call it in
class Potting < ApplicationRecord
belongs_to :batch, inverse_of: :pottings
validates :plant_count, :completed_at, presence: true
enum germination_result: { light: 0, medium: 1, full: 2 }
def pottings_completed_at
"Week #{completed_at.strftime('%U').to_i}/#{completed_at.strftime('%Y').to_i}"
end
def completed_at
super || Time.zone.today
end
def completed_at_week
completed_at.strftime('%U')
end
def current_period
return unless completed_at_week.between?(period.start_week, period.finish_week)
index_days_till_sellables_on_period_id
end
def period_days
plant.days_till_sellables.find_by(period: :current_period).&current_period.days
end
def ready_for_sale
completed_at + period_days
end
end
I've added more Code below to give better context for classes
class DaysTillSellable < ApplicationRecord
belongs_to :period, inverse_of: :days_till_sellables, foreign_key: :period_id
belongs_to :plant, inverse_of: :days_till_sellables, foreign_key: :plant_id
validates :days, presence: true
end
.
class Period < ApplicationRecord
has_many :days_till_sellables, dependent: :destroy, inverse_of: :period
belongs_to :organization
.
class Plant < ApplicationRecord
has_many :batches, dependent: :destroy
has_many :goals, dependent: :destroy
has_many :days_till_sellables, dependent: :destroy, inverse_of: :plant
belongs_to :organization
accepts_nested_attributes_for :days_till_sellables, allow_destroy: true
validates :genus, :species, :period_id, presence: true
end
I think you are looking for:
class Potting
belongs_to :plant, through: :batch
...
end

How do i associate a model_id to a nested model of third level?

my model structure looks like this
resources :genres do
resources :stories do
resources :episodes do
resources :comments
end
end
end
I have already added an episode_id to the comments table. However when i added genre_id and story_id to comments table and check for it in the console, genre_id and story_id was given Nil.
`
Comment.rb
class Comment < ApplicationRecord
belongs_to :episode
belongs_to :story
belongs_to :user
end
Genre.rb
class Genre < ApplicationRecord
belongs_to :user
has_many :stories, dependent: :destroy
end
Story.rb
class Story < ApplicationRecord
belongs_to :genre
belongs_to :user
has_many :comments
has_many :episodes, dependent: :destroy
has_attached_file :image, size: { less_than: 1.megabyte }, styles:{ medium: "300x300#", wide: "200x400#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
scope :of_followed_users, -> (following_users) { where user_id: following_users }
end
Firstly, you can't associate comment belongs to more models without set otional: true, because your comment will require ids for all associated models.
class Comment < ApplicationRecord
belongs_to :episode, optional: true
belongs_to :story, optional: true
belongs_to :user
end
I sugest you to use this example.
change comment table with this migration
def change
add_column :comments, :commentable_type, :string
add_column :comments, :commentable_id, :integer
add_column :comments, :user_id, :integer
end
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end
class User < ApplicationRecord
has_many :comments
end
class Genre < ApplicationRecord
...
has_many :comments, as: :commentable, depented: :destroy
end
class Story < ApplicationRecord
...
has_many :comments, as: :commentable, depented: :destroy
end
now you can Story.last.comments.new => .., commentable_id: story_last_id, commentable_type: 'Story'
more about polymorphic associations

Rails custom validation on has_many association

Given the following models:
Business
class Business < ActiveRecord::Base
## OWNERS / EMPLOYEES
has_many :business_users, as: :business, dependent: :destroy
has_many :users, through: :business_users
accepts_nested_attributes_for :business_users, allow_destroy: true, reject_if: lambda { |a| a[:user_id].blank? || a[:role].blank? }
end
BusinessUser
class BusinessUser < ActiveRecord::Base
belongs_to :business, polymorphic: true
belongs_to :user
enum role: {:owner => 0, :employee => 1, :admin => 2}
validates_presence_of :user
validates :user, uniqueness: { scope: :business, message: "Each user can only have one role" }
end
User
class User < ActiveRecord::Base
has_many :business_users
has_many :businesses, through: :business_users, source_type: Business, source: :business
end
How can i make sure that each business has at least one business_user with role :owner?

Resources