i have following code:
class Item < ActiveRecord::Base
belongs_to :user
has_many :transactions
#scope :active, lambda??
end
class Transaction < ActiveRecord::Base
belongs_to :user
belongs_to :item
scope :active, where("status = 0")
end
class User < ActiveRecord::Base
has_many :items
has_many :transactions
end
i wish to build a scope in model Item to retrieve only records with active transactions, for example:
User.find(1).items.active
I found the answer. it shoud be this way:
scope :active, joins(:transactions)
Transaction.active
The answer was here: http://asciicasts.com/episodes/215-advanced-queries-in-rails-3
Related
I'm trying to get some records from table, but i don't know how to build this query.
I have some models.
class Request < ActiveRecord::Base
has_many :notifications, as: :source
has_many :decisions, dependent: :destroy
end
class Notification < ActiveRecord::Base
belongs_to :source, polymorphic: true
end
class Decision < ActiveRecord::Base
has_many :notifications, as: :source
belongs_to :request
end
So, I need to get all Notifications where source = some_request or source.request = some_request
Isn't it something simple as -
some_request.notifications
# or
some_decision.notifications
and if source is combination of request & decision then
notifications_ids = some_request.notifications.pluck(:id) +
some_decision.notifications.pluck(:id)
Notification.find(notifications_ids)
Your query should be Notification.where(source_id: some_request.id, source_type: 'Request')
Refer Active record association
Suppose I have a result from a query:
allCourses = Course.all
Then I also have another set:
myCourses = current_user.courses.all
How can I get a set of items that are in allCourses and NOT in myCourses?
Here are the models:
class Student < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :courses, through: :student_enrollments
end
class Course < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :students, through: :student_enrollments
end
class StudentEnrollment < ActiveRecord::Base
belongs_to :student
belongs_to :course
end
I can always write raw SQL script to achieve the result, but I prefer to find a Rails way to do it.
Thanks
Assume your fk is user_id.
ohterCourses = Course.where('user_id != ?', current_user.id)
Seems that made some mistake in the model
class LineItemAddons < ActiveRecord::Base
belongs_to :line_item
has_one :addon_type_value
attr_accessible :quantity, :received, :snapshot_price, :status, :line_item_id, :addon_type_value_id
class LineItem < ActiveRecord::Base
belongs_to :design
belongs_to :cart
belongs_to :designer_order
belongs_to :return
belongs_to :return_designer_order
has_many :line_item_addons
class AddonTypeValue < ActiveRecord::Base
belongs_to :design
belongs_to :addon_type
belongs_to :line_item_addon
attr_accessible :description, :name, :position, :price, :addon_type_id, :design_id, :quantity
Getting unintialized constant for line_item_addons
I think it should be
class AddonTypeValue < ActiveRecord::Base
belongs_to :design
belongs_to :addon_type
belongs_to :line_item_addons
...
You don't need to describe association belongs_to with has_one.
http://guides.rubyonrails.org/association_basics.html#the-has-one-association
Try to remove belongs_to :line_item_addon from AddonTypeValue model
I made a mistake while generating the model
the class name should LineItemAddon not LineItemAddons
Sorry Guys
I am trying to select groups which the #current_user is NOT a member of
The relevant parts of my models are as follows:
class Group < ActiveRecord::Base
belongs_to :user
has_many :group_memberships
has_many :members, :class_name => "User", :through=>:group_memberships
...
class User < ActiveRecord::Base
has_many :group_memberships, :foreign_key => 'member_id'
has_many :groups, :through => :group_memberships
...
class GroupMembership < ActiveRecord::Base
belongs_to :member, :class_name=>"User"
belongs_to :group
end
Thanks!
You could try and do something like:
#groups = Group.where("id NOT IN (?)", current_user.groups)
More information can be found in the Active Record Query.
have a method like this:
def group_ids_not_a_member_of
# get all group_ids and subtract out the ids that he is a member of
Group.pluck('id') - current_user.groups.map(&:id)
end
then
user.group_ids_not_a_member_of
I have three models Agreement, Service and Price.
class Agreement < ActiveRecord::Base
has_many :prices, as: :priceable
end
class Service < ActiveRecord::Base
has_many :prices, as: :priceable
end
class Price < ActiveRecord::Base
attr_accessible :price, :price_currency, :priceable_id, :priceable_type
belongs_to :priceable, polymorphic: true
end
But I have two price types in service customer_price and agency_price. Agreement hasn't price type. I want to model something like below. How it is possible?
class Agreement < ActiveRecord::Base
has_many :prices, as: :priceable
end
class Service < ActiveRecord::Base
has_many :customer_prices, as: :priceable # I think I should write something here
has_many :agency_prices, as: :priceable # I think I should write something here
end
class Price < ActiveRecord::Base
attr_accessible :price, :price_currency, :priceable_id, :priceable_type
belongs_to :priceable, polymorphic: true
end
What is best approach? May be I should make two price model like AgreementPrice and ServicePrice. Best Regards.
Making two models wouldn't help you actually.
I think you only need to set a specific foreign_key to your relations.
has_many :customer_price, as: :priceable, :foreign_key => customer_price_id
has_many :agency_price, as: :priceable, :foreign_key => agency_price_id
Those two fields must be added in your schema.