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"}
Related
As i mentioned on the question..
I know this error and i know how associations work
In my case i have ProductRepository model which belongs_to :category
So of course when i'm trying to delete this category which this ProductRepository object belongs to i should get this error and it's totally fine and i know how to handle this exception
Now let's say that i have a many models which referring to categories like
Product, ProductRepositoy, Post, etc...
My question here, how can i get a list of this objects which belongs to categories, or even get a class name array or whatever
So when i handle this exception i can send a message to ask the user to check and delete the following before deleting this category ?
Example:
Once user delete category... if it has any items which belongs to it
he should get a message saying Please check the following (Posts, Products, etc..)
For who is going to answer, all i need is how to get this objects or even class names only
And i can do the rest
Thanks in advance :)
Here is the file, after removing what isn't necessary for the question
category.rb
class Category < ActiveRecord::Base
has_many :brands, :dependent => :destroy
has_many :products, :dependent => :destroy
end
product_respository.rb
class ProductRepository < ActiveRecord::Base
belongs_to :category
has_many :unity_folders, as: :product, dependent: :destroy
validates :name, :price, :barcode, presence: true
validates :barcode, uniqueness: true
end
product.rb
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :brand
belongs_to :organization
has_many :unity_folders, as: :product, dependent: :destroy
has_one :product3d
belongs_to :shop
end
I'm wondering if there is a cleaner way to validate multiple relationships in rails. I don't mean validating an association, rather ensuring relationship integrity between two or more belongs_to associations. Here is some code as an example:
class User < ActiveRecord::Base
has_many :products, inverse_of: :user
has_many :purchases, inverse_of: :user
end
class Purchase < ActiveRecord::Base
belongs_to :user, inverse_of: :purchases
has_many :products, inverse_of: :purchase
end
class Product < ActiveRecord::Base
belongs_to :user, inverse_of: :products
belongs_to :purchase, inverse_of: :products
validates :user, :purchase, presence: true
validate :purchase_user
private
def purchase_user
errors.add(:purchase, 'purchase user does not match user') if user != purchase.user
end
end
The purchase_user validation method here checks that the user is the same as purchase.user and adds an error if they are not.
I could change it to use :inclusion like so:
validates :purchase, inclusion: { in: proc { |record| record.user.purchases } }
But that seems even more inefficient, any suggestions?
I have 3 models. Rom::Favorite, Rom::Card, User. I am having an issue creating the User has_many rom_cards through rom_favorites
Here are my relevant parts of my models
Rom::Card
class Rom::Card < ActiveRecord::Base
has_many :rom_favorites, class_name: "Rom::Favorite", foreign_key: "rom_card_id", dependent: :destroy
self.table_name = "rom_cards"
end
User
class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :role
has_many :rom_favorites, class_name: "Rom::Favorite", dependent: :destroy
has_many :rom_cards, class_name: "Rom::Card", through: :rom_favorites, class_name: "Rom::Favorite"
end
Rom::Favorite
class Rom::Favorite < ActiveRecord::Base
attr_accessible :rom_card_id, :user_id
belongs_to :user
belongs_to :rom_card, class_name: "Rom::Card"
validates :user, presence: true
validates :rom_card, presence: true
validates :rom_card_id, :uniqueness => {:scope => :user_id}
self.table_name = "rom_favorites"
end
All of the utility methods that come along with associations work except
a = User.find(1)
a.rom_cards
The call a.rom_cards returns an empty array and it seems to run this SQL query
SELECT "rom_favorites".* FROM "rom_favorites" INNER JOIN "rom_favorites" "rom_favorites_rom_cards_join" ON "rom_favorites"."id" = "rom_favorites_rom_cards_join"."rom_card_id" WHERE "rom_favorites_rom_cards_join"."user_id" = 1
I am not strong in SQL, but I think this seems correct.
I know a.rom_cards should return 2 cards because a.rom_favorites returns 2 favorites, and in those favorites are card_id's that exist.
The call that should allow rom_cards is the following
has_many :rom_cards, class_name: "Rom::Card", through: :rom_favorites, class_name: "Rom::Favorite"
I feel like the issue has something to do with the fact that it is trying to find the Users cards through favorites and it is looking for card_id (because I specified the class Rom::Card) instead of rom_card_id. But I could be wrong, not exactly sure.
You are duplicating the key class_name in the association hash. There is no need to write class_name: "Rom::Favorite" because by using through: :rom_favorites it will use the configuration options of has_many :rom_favorites.
Try with:
has_many :rom_cards, class_name: "Rom::Card", through: :rom_favorites
Is it acceptable to have a has_and_belongs_to_many: association as well as a has_many through: relationship on models? For instance, I have three models, Facility, Athlete, and Event.
An Athlete has and belongs to many Facilities, and vice versa. I need to allow for Facilities to create Events that belong to both the facility and the athlete. So I have the following.
class Athlete < ActiveRecord::Base
attr_accessible :age, :email, :facility_id, :name
has_and_belongs_to_many :facilities
has_many :events
has_many :facilities, :through => :events
end
class Facility < ActiveRecord::Base
attr_accessible :name, :contact_email, :contact_name, :telephone
has_and_belongs_to_many :athletes
has_many :events
has_many :athletes, :through => :events
end
class Event < ActiveRecord::Base
attr_accessible :athlete_id, :facility_id, :rfd250, :rfd50, :rfd85
belongs_to :athlete
belongs_to :facility
end
My hope with this setup is that from Facilities#Show, I can create new Athletes associated with a particular Facility (athletes_facilities table). After which I can display only those athletes associated with the Facility, and create new Events for them.
Is this the wrong way to approach the problem?
I'm creating trips in my rails app and i want to add categories to those trips. The categories are stored in the Categories table in my DB and the user may select which categories are suitable for the trip. So multiple categories a trip can be used.
Although i'm a noob i figured some things out with some help of the RoR guides about this subject. Now, i've got a 3rd table tripcategories which will have to hold the trip_id and the category_id. Right? With that i've got the following models:
trip.rb:
class Trip < ActiveRecord::Base
attr_accessible :description, :title, :user_id, :triplocations_attributes, :photo
has_many :triplocations, :dependent => :destroy
has_many :tripcategories
has_many :categories, :through => :tripcategories
accepts_nested_attributes_for :triplocations, allow_destroy: true
end
category.rb:
class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
belongs_to :trip, :through => :tripcategories
end
tripcategory.rb:
class Tripcategory < ActiveRecord::Base
attr_accessible :category_id, :trip_id
belongs_to :trip
belongs_to :category
end
When i'm trying it this way and trying to call trip.categories in my trip index it says "Unknown key: through". Am i doing something horribly wrong or am i missing the bigger picture?
Thanks in advance!
class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
has_many :trips, :through => :tripcategories
end