Reputation: 1799
I would really appreciate if someoneone could help me write a scope displaying the number of females (women) attending an event.
Models
user.rb
belongs_to :category_gender
has_many :payments
category_gender.rb
has_many :users
event.rb
belongs_to :user
has_many :payments
payment.rb
belongs_to :user
belongs_to :event
Terminal
event = Event.find(7)
event_payments = event.payments
2.3.0 :054 > event_payments
[
[0] #<Payment:0x007fea72ac5b70> {
:id => 6,
:email => "[email protected]",
:user_id => 4,
:reference => "SPz_ruZ5om",
:created_at => Wed, 16 Nov 2016 13:52:23 UTC +00:00,
:updated_at => Wed, 16 Nov 2016 13:52:23 UTC +00:00,
:event_id => 7,
:stripe_customer_id => "cus_9YuZZAniFCZVxn",
:stripe_payment_id => "ch_19GTpnAAe71J3vK0aj1gKryO",
:event_payment_date => Wed, 16 Nov 2016 13:52:23 UTC +00:00,
:status => "success"
}
[1] #<Payment:0x007fea72ac5b70> {
:id => 7,
:email => "[email protected]",
:user_id => 5,
:reference => "SPz_mnX4ul",
:created_at => Wed, 16 Nov 2016 13:52:23 UTC +00:00,
:updated_at => Wed, 16 Nov 2016 13:52:23 UTC +00:00,
:event_id => 7,
:stripe_customer_id => "cus_6FhLLMotYCCYvn",
:stripe_payment_id => "ch_20GTunDDt87J3vK1sp4gPry9",
:event_payment_date => Wed, 16 Nov 2016 13:52:23 UTC +00:00,
:status => "success"
}
]
How do I write a scope to find all the payments made by females under an event?
If I write the below I can find the first payment made by a woman:
event.payments.first.user.category_gender.name
2.3.0 :026 > event.payments.first.user.category_gender.name
Payment Load (0.2ms) SELECT "payments".* FROM "payments" WHERE "payments"."event_id" = ? ORDER BY "payments"."id" ASC LIMIT 1 [["event_id", 7]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
CategoryGender Load (0.1ms) SELECT "category_genders".* FROM "category_genders" WHERE "category_genders"."id" = ? LIMIT 1 [["id", 2]]
=> "Female"
I tried the below scope to display all the payments made by females for an event, but no luck - tried a method as well but no luck. Your help and explanation would be much appreciated on who to write this as a scope and a method:
event.rb
scope :females, -> { joins(payment: :category_gender).where('category_genders.name' => "Female") }
def females
self.payments.where('user.category_gender.name = ?', "Female")
end
error message:
2.3.0 :004 > event.females
Payment Load (1.7ms) SELECT "payments".* FROM "payments" WHERE "payments"."event_id" = ? AND (user.category_gender.name = 'Female') [["event_id", 7]]
SQLite3::SQLException: no such column: user.category_gender.name: SELECT "payments".* FROM "payments" WHERE "payments"."event_id" = ? AND (user.category_gender.name = 'Female')
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: user.category_gender.name: SELECT "payments".* FROM "payments" WHERE "payments"."event_id" = ? AND (user.category_gender.name = 'Female')
Upvotes: 1
Views: 26
Reputation: 52357
class Payment
scope :by_females, -> { joins(user: :category_gender).where(category_genders: { name: 'Female' }) }
end
Now easy usage:
event.payments.by_females
Upvotes: 3