How to scope validation to specific model in polymorphic association.? - ruby-on-rails

I have two model User and Investment and one polymorhic model Address
class User < ActiveRecord::Base
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :address
end
class Investment < ActiveRecord::Base
has_many :addresses, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :addresses, reject_if: lambda { |v| v['address'].blank? } && :address_blank, :allow_destroy => true
end
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
validates :address, presence: true
end
now validates :address, presence: true will applicable to both Investment as well as User
but i want it to applicable to only Investment not to User. so how do i do that.
Thanks.

in class Investment add
validates :address_id, presence: true
and remove bellow from class Address
validates :address, presence: true

class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
validates :address, presence: true, if: :investment?
protected
def investment?
addressable_type == 'Investment'
end
end

Related

Rails: create relationship between user posts comments and likes

I am trying to implement likes into my rails app.
like model
class Like < ApplicationRecord
belongs_to :comment
belongs_to :post
belongs_to :user
validates :user_id, uniqueness: {scope: :post_id}
validates :user_id, uniqueness: {scope: :comment_id}
end
user model
class User < ApplicationRecord
has_secure_password
has_one :profile, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :posts, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :liked_comments, :through => :likes, :source => :comment, dependent: :destroy
has_many :liked_posts, :through => :likes, :source => :post, dependent: :destroy
has_one_attached :avatar
validates :username, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, length: { minimum: 8 }
validates :password, format: { with: /\A[a-zA-Z0-9!##$%^&*()_]+\z/ }
# validates :password, confirmation: true
# validates :password_confirmation, presence: true
end
post & comment model
class Post < ApplicationRecord
belongs_to :category
belongs_to :user
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :liking_users, :through => :likes, :source => :user
has_many_attached :images
def liked?(user)
!!self.likes.find{|like| like.user_id == user.id}
end
end
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
has_many :likes, dependent: :destroy
has_many :liking_users, :through => :likes, :source => :user
has_many_attached :images
def liked?(user)
!!self.likes.find{|like| like.user_id == user.id}
end
end
The issue I'm having is when I run my seed data I get one of two errors NoMethodError: undefined method "marked_for_destruction?" for false:FalseClass if set up like so:
#like0 = Like.create!(
user: #admin0,
post: #post3 & #post5 & #post6 & #post8 & #post9,
comment: #comment1,
username: #admin0.username
)
or NotNullViolation: ERROR: null value in column "comment_id" violates not-null constraint if set up like so:
class Like < ApplicationRecord
belongs_to :comment, optional: true
belongs_to :post, optional: true
belongs_to :user
validates :user_id, uniqueness: {scope: :post_id}
validates :user_id, uniqueness: {scope: :comment_id}
end
#like0 = Like.create!(
user: #admin0,
post: #post3,
comment: #comment1,
username: #admin0.username
)
my question is how can I better set up the relationship between these 4 models so as to not get these errors?

Rails foreign_key not id

I have two tables
class Sku < ApplicationRecord
validates :sku, :supplier_code, :name, :price, presence: true
belongs_to :supplier, class_name: 'Supplier', foreign_key: 'code'
end
and
class Supplier < ApplicationRecord
validates :code, :name, presence: true
has_many :skies, class_name: 'Sku'
end
i set foreign_key as a code field
but when i tried to create sku with supplier_code which i set to supplier
Supplier.create(code:4,name:2) => OK
i have got an error {:supplier=>[{:error=>:blank}]},
Sku.create(name:2,price:2,sku:3,supplier_code:4).errors
You must need to define on another model
class Sku < ApplicationRecord
validates :sku, :supplier_code, :name, :price, presence: true
belongs_to :supplier, class_name: 'Supplier', foreign_key: 'code'
end
and
class Supplier < ApplicationRecord
validates :code, :name, presence: true
has_many :skies, class_name: 'Sku', primary_key: 'id', foreign_key: 'code'
end

Add Model Validation {Rails}

I have two model association as follows:
Survey_question and survey_answer.
Survey_question Model
class SurveyAnswer < ApplicationRecord
belongs_to :survey_question
validates_presence_of :answer_content, if: "answer_type!=Radio Button"
end
Survey_question Model
class SurveyQuestion < ApplicationRecord
belongs_to :user
belongs_to :survey
has_many :survey_answers
accepts_nested_attributes_for :survey_answers, allow_destroy: true
has_many :survey_responses
accepts_nested_attributes_for :survey_responses
validates :question_content, presence: true
validates :answer_type, presence: true
end
I just to validate presence of survey_answer's answer_content when survey_question's answer_type is "Radio Button" ? how can I achieve this ??
thanks in advance.
I'm not sure if you are looking for an if or unless based on using != in your example, but try the following:
validates_presence_of :answer_content, if: ->(answer) { answer.survey_question.answer_type == 'Radio Button' }

Rails 4 - Validates uniqueness with has_many through

I have these three models:
User:
class User < ActiveRecord::Base
validates :name, presence: true
validates :surname, presence: true
validates :email, presence: true, format: { with: /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
has_many :permissions, dependent: :destroy
has_many :stores, through: :permissions
end
Store:
class Store < ActiveRecord::Base
validates :name, presence: true
validates :description, presence: true
has_many :permissions
has_many :users, through: :permissions
end
Permission:
class Permission < ActiveRecord::Base
belongs_to :user
belongs_to :store
end
How can i validate the uniqueness of the user.email based on the store.id?
You don't.
You should validate the uniqueness of the users email in User. And validate the uniqueness of the user_id and store_id in Permission.
class User < ApplicationRecord
# ...
validates_uniqueness_of :email
end
class Permission < ApplicationRecord
validates_uniqueness_of :user_id, scope: 'store_id'
end
This allows a user to have permissions for multiple stores - but does not allow duplicates. In general when linking records together use id's - not something like emails.

Second Level Model

I have Locations, Order and Event Models.
class Order < ActiveRecord::Base
belongs_to :event
validates :first_name, presence: true
validates :last_name, presence: true
validates :amount, presence: true, :numericality => {:only_integer=>true, :greater_than =>0}
end
class Event < ActiveRecord::Base
belongs_to :location
validates :title, presence: true
validates :price, presence: true, :numericality => {:greater_than =>0}
end
class Location < ActiveRecord::Base
has_many :events, dependent: :destroy
has_many :orders, through: :events
validates :title, presence: true
validates :size, presence: true, :numericality => {:only_integer=>true,:greater_than =>0}
end
I want to check, in the orders create controller, how many seats the location has.
#orders.events returns me the right event.
if I ask for #order.event.location the return is always "#".
Is this a security issue? I also added in the Location model:
has_many: orders, through: events

Resources