Second Level Model - ruby-on-rails

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

Related

How to exclude the validation in case of the specific nested attributes?

I'm using one address model with polymorphic.
Class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true, touch: true
...
validates :street_address_1, presence: {with true, message:'cannot be blank'}
validates :street_address_2, presence: {with true, message:'cannot be blank'}
validates :city, presence: {with true, message:'cannot be blank'}
validates :locale, presence: {with true, message:'cannot be blank'}
validates :postal_code, presence: {with true, message:'cannot be blank'}
validates :country, presence: {with true, message:'cannot be blank'}
...
end
Company model
class Company < ApplicationRecord
...
has_many :physical_addresses, :as=> addressable, dependent: :destroy
accepts_nested_attributes_for :physical_addresses, allow_destroy: true
...
end
User model
class User < ApplicationRecord
...
has_many :physical_addresses, :as=> addressable, dependent: :destroy
accepts_nested_attributes_for :physical_addresses, allow_destroy: true
...
end
I'm using the nested attribute for creating or updating the User and Company controller.
I'd like to apply the address validation only for User creating and updating. But the companies don't have to be applied the address validation.
Is there any best way to solve this issue?
Write a validator class.
Skip the validations if addressable refers to a Company.
def validate(address)
return unless address.addressable.kind_of?(User)
...
end

How to created new record on has_one for two model on the same time on Rails?

I need to create new record for two models that belong to the same model on one method.
This is my Model
class Promotion < ApplicationRecord
has_one :promotion_thai ,dependent: :destroy
has_one :promotion_eng ,dependent: :destroy
end
class PromotionThai < ApplicationRecord
belongs_to :promotion
mount_uploader :long_banner, PromotionImageUploader
mount_uploader :square_banner, PromotionImageUploader
mount_uploader :details_image, PromotionImageUploader
validates :promotion_id, presence: true
validates :title, presence: true
validates :description, presence: true
#validates :long_banner, presence: true
#validates :square_banner, presence: true
end
class PromotionEng < ApplicationRecord
belongs_to :promotion
mount_uploader :long_banner, PromotionImageUploader
mount_uploader :square_banner, PromotionImageUploader
mount_uploader :details_image, PromotionImageUploader
validates :promotion_id, presence: true
validates :title, presence: true
validates :description, presence: true
validates :long_banner, presence: true
validates :square_banner, presence: true
end
This is my controller method
def create
promotion = Promotion.new
promotion.build_promotion_eng(promotion_eng_params).build_promotion_thai(promotion_thai_params)
if promotion.save
flash[:success] = 'Success Created Promotion'
redirect_to admins_promotions_path
else
errors_message = promotion.errors.full_messages.join(', ')
redirect_to admins_promotion_new_path, :flash => { :error => errors_message }
end
end
Then when i submit the form i always got this error
undefined method `build_promotion_thai' for #<PromotionEng:0x007f9fdbcb0250> Did you mean? build_promotion
On this line
promotion.build_promotion_eng(promotion_eng_params).build_promotion_thai(promotion_thai_params)
How can i fix this kind of problem?
Thanks!
That's because build_promotion_eng(promotion_eng_params) returns an PromotionEng instance.
This should work fine.
promotion.build_promotion_eng(promotion_eng_params)
promotion.build_promotion_thai(promotion_thai_params)

Validates uniqueness in a one-to-many association

I have an Album model and Track model. I want to make sure that the name of each track on the album they belong to are unique. I tried using this validation in my Track model
validates :name, presence: true, uniqueness: true, scope: :album_id
but get the error: Unknown validator: 'ScopeValidator'
What am I doing wrong?
class Album < ActiveRecord::Base
has_many :tracks
accepts_nested_attributes_for :tracks, :reject_if => :all_blank, :allow_destroy => true
validates :name, presence: true, uniqueness: true
end
class Track < ActiveRecord::Base
belongs_to :album
validates :name, presence: true, uniqueness: true, scope: :album_id
end
You need to scope the uniqueness, not put it as a separate argument.
class Track < ActiveRecord::Base
belongs_to :album
validates :name, presence: true, uniqueness: { scope: :album_id }
end

How to scope validation to specific model in polymorphic association.?

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

Choose from foreign keys in rails admin not working

Following is my models:
class Poll < ActiveRecord::Base
attr_accessible :published, :title
validates :published, :presence => true
validates :title, :presence => true,
:length => { :minimum => 10 }
has_many :choice, :dependent => :destroy
end
class Choice < ActiveRecord::Base
belongs_to :poll
attr_accessible :choice_text, :votes
validates :choice_text, :presence => true
end
I then tried to install the rails admin. I was able to create the choices and polls in the admin, but i was unable to associate a choice with a poll and vice versa.
How can i do it?
First of all in has_many class name should be in plural:
has_many :choices
And you should add attr_accessible poll_id or choice_ids for Model from which you want to edit this association. Or just delete all attr_accessible for first try.
class Poll < ActiveRecord::Base
attr_accessible :published, :title, choice_ids
validates :published, :presence => true
validates :title, :presence => true, :length => { :minimum => 10 }
has_many :choices, :dependent => :destroy
end
class Choice < ActiveRecord::Base
belongs_to :poll
attr_accessible :choice_text, :votes, :poll_id
validates :choice_text, :presence => true
end
There is no attr_accessible in Rails 4. Use accepts_nested_attributes_for instead. More info:
https://github.com/sferik/rails_admin/wiki/Belongs-to-association

Resources