I have two model product and variant.
Product.rb => model.
class Product < ActiveRecord::Base
has_many :variants, dependent: :destroy
end
variant.rb => model
class Variant < ActiveRecord::Base
belongs_to :product
validates :product_id, presence: true
end
I want to validate product_id validation when admin create record from variant form, not when admin create record from product model using has_many relationship.
Thanks in advance
Related
I'm facing a difficult to associate things on Rails. In this case, I have a model for locals, and a model to identifier a user token ( that came from another APi). A place has a rating ( a score from 0 to 5 ) associated with it, and every user has many ratings but just one for each place. Trying to do this I create a new model that has ratings attribute and I want to associate a rating id to just one place id.
# Description of User Identifier Class
class UserIdentifier < ApplicationRecord
has_many :favorite_locals, dependent: :destroy
has_many :user_rate, dependent: :destroy
validates :identifier, presence: true
validates_numericality_of :identifier
validates_uniqueness_of :identifier
def self.find_favorites(params)
UserIdentifier.find(params).favorite_locals
end
end
# Model of Users Rates
class UserRate < ApplicationRecord
belongs_to :user_identifier
validates :rating, numericality: true
validates_numericality_of :rating, less_than_or_equal_to: 5
validates_numericality_of :rating, greater_than_or_equal_to: 0
validates :user_identifier, presence: true
end
First, you have a typo in the has_many :user_rate, dependent: :destroy. The association should be named user_rates, so the correct code for UserIdentifier model is:
class UserIdentifier < ApplicationRecord
has_many :favorite_locals, dependent: :destroy
has_many :user_rates, dependent: :destroy
# ...
end
Second, it's not clear how the "place" entity is named in your project. If it's FavoriteLocal then this is the code you need:
class UserRate < ApplicationRecord
belongs_to :user_identifier
belongs_to :favorite_local
# ...
end
If this is another model just define the belongs to association right below belongs_to :user_identifier. I hope you got the idea.
I've been struggling with setting up a has_many/through relationship when one model object is being created through nested attributes using Factory Girl.
class CommercialInvoice < ActiveRecord::Base
has_many :shipment_commercial_invoices, :dependent => :destroy
has_many :shipments, :through => :shipment_commercial_invoices
end
class Shipment < ActiveRecord::Base
has_many :shipment_commercial_invoices, :dependent => :destroy
has_many :commercial_invoices, :through => :shipment_commercial_invoices
end
class ShipmentCommercialInvoices < ActiveRecord::Base
attr_accessible :job_id, :detail_id
belongs_to :shipment
belongs_to :commercial_invoice
end
The Shipment model is created using nested attributes inside another model supplier_invoice
class SupplierInvoice < ActiveRecord::Base
has_many :shipments, :dependent => :destroy
accepts_nested_attributes_for :shipments, :allow_destroy => true
end
class Shipment < ActiveRecord::Base
belongs_to :supplier_invoice
end
My Factories
factory :supplier_invoice do do
shipments_attributes do
shipments_attributes = []
2.times do # 2 shipments per supplier invoice
shipments_attributes << attributes_with_foreign_keys(:shipment)
end
shipments_attributes
end
end
factory :commercial_invoice do
after(:create) do |commercial_invoice, evaluator|
commercial_invoice.shipments << FactoryGirl.create(:supplier_invoice).shipments
end
end
factory :shipments_commercial_invoice do
shipment
commercial_invoice
comm_invoiced true # A boolean attribute that i want to be true
end
Now the problem in whenever i create a new commercial invoice, the associated shipment_commercial_invoice's attribute comm_invoiced is always false. I am not able to figure out what am i doing wrong. Is there any problem with how i defined factories? Thanks in advance.
i have a user model that has many designs and also has_many design_request through designs
Now my design_request model has_one order. how can i get all the design requests with the order state == "paid" for a user
my user model is below
##User Model
has_many :designs
has_many :design_requests, through: :designs
##Design Model
has_many :design_requests
##Design Request Model
belongs_to :design
belongs_to :user
validates :business_name, :design_id, presence: true
delegate :name, to: :design
has_one :order, as: :item
have you tried something like:
user.design_requests.includes(:order).where(orders: {state: "paid"})
Does this work?
user.design_requests.select {|dr| dr.order.state == "paid"}
I have three models as follows:
class User < ActiveRecord::Base
...
has_many :feeds
...
end
class Project < ActiceRecord::Base
...
has_many :feeds
has_many :users, through: :feeds
...
end
class Feed < ActiveRecord::Base
...
belongs_to :user
belongs_to :project
...
end
I want to model the situation where a user can have a maximum of one feed per project. I know that I can do this check in a custom validator within the Feed class, but is there a way to model this using only ActiveRecord associations?
You can do that on Feed.rb:
validates :user_id, :uniqueness => {:scope => :project_id}
My models look something like this:
class User < ActiveRecord::Base
attr_accessible: :name
has_many :reviews
end
class Product < ActiveRecord::Base
attr_accessible: :name
has_many :reviews
end
class Review < ActiveRecord::Base
attr_accessible: :comment
belongs_to :user
belongs_to :product
validates :user_id, :presence => true
validates :product_id, :presence => true
end
I am trying to figure out what the best way is to create a new Review, given that :user_id and :product_id are not attr_accessible. Normally, I would just create the review through the association ( #user.reviews.create ) to set the :user_id automatically, but in this case I am unsure how to also set the product_id.
My understanding is that if I do #user.reviews.create(params), all non attr_accessible params will be ignored.
You can do:
#user.reviews.create(params[:new_review])
...or similar. You can also use nested attributes:
class User < ActiveRecord::Base
has_many :reviews
accepts_nested_attributes_for :reviews
...
See "Nested Attributes Examples" on http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html.
It seems you would like to implement a many-to-many relationship between a User and Product model, with a Review model serving as a join table to connect the two with an added comment string. This can be accomplished with a has many through association in Rails. Start by reading the Rails Guides on Associations.
When setting up your Review model, add foreign keys for the User and Product:
rails generate model review user_id:integer product_id:integer
And set up your associations as follows:
class User < ActiveRecord::Base
has_many :reviews
has_many :products, through: :reviews
end
class Product < ActiveRecord::Base
has_many :reviews
has_many :users, through: :reviews
end
class Review < ActiveRecord::Base
# has comment string attribute
belongs_to :user
belongs_to :product
end
This will allow you to make calls such as:
user.products << Product.first
user.reviews.first.comment = 'My first comment!'
Here's how you would create a review:
#user = current_user
product = Product.find(params[:id])
#user.reviews.create(product: product)