
Stephen Sizer
Joined
Activity
Posted in Searchkick Elasticsearch filtering
Does anyone know of any good tutorials showing how to add filters for searching?
Thanks in advance.
I have a Lesson model that has an order field of type integer. What i want to do is find the previous lesson.
Lesson.where("order < ?" , 8)
This causes the following error. Any help would be appreciated.
Lesson Load (1.9ms) SELECT "lessons".* FROM "lessons" WHERE (order < 8) LIMIT $1 [["LIMIT", 11]]
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "order"
LINE 1: SELECT "lessons".* FROM "lessons" WHERE (order < 8) LIMIT...
^
: SELECT "lessons".* FROM "lessons" WHERE (order < 8) LIMIT $1
I am currently building an LMS and am looking to add in the ability for the user to navigate to the next lesson.
In the admin back end when a teacher adds a lesson they are able to give it a position, this helps lay the lessons out. I sort on both position and then by title. So the learner can see the whole list of lessons, including topics within a lesson.
Now when the user views a lesson, I want to have the functionality for the learner to be able to click next lesson button. This will mark the current lesson as viewed and also navigate to the next lesson.
What is the best way to go about this?
I have looked and have seen the following suggestions
@next_lesson = @course.lessons.where("position > ?", @lesson.position).order(:position).first
This will work as long as every lesson has a unique position set. Sometimes this may not be the case, imagine a scenario where the teacher thinks of a lesson which would be better suited to the start of the course and he wants to add this to the beginning adding the postion of 2 this would fail as there is already a lesson with this value. Would they then need to change every lesson position value to add this position in?
Not sure what the best way to do this would be? Suggestions are welcome, especially if I have missed something very obvious.
Models for Course, Lesson and Topic are shown below, but this is just standard stuff.
class Course < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
has_many :lessons
end
class Lesson < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
belongs_to :course
has_many :topics
end
class Topic < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
belongs_to :lesson
end
Anyone done this? and whats the best way to go about it?
Thanks in advance
Posted in How do I transfer user data on delete
Posted in How do I transfer user data on delete
I have a users table
Then i have the following tables that also the user_id column
Courses
Notes
Comments
Questions
Answers
What I want to be able to do is. If a member of the team leaves I want to be able to assign all courses, notes, comments, questions and answers over to another user that already exists.
Thanks,
Steve
Posted in How do I transfer user data on delete
Hope that makes sense? Thanks in advance
This is correct :)
Hi Jack,
Thanks for the reply.
that code would work for a project being 2 weeks old. Some projects may run for a year and i want to run some code every week.
To give you more context...
Every week I want to create a review for the project. So after 1 whole week a review gets created for the project manager to fill out. After 2 weeks, 3 weeks, 4 weeks ... 27 weeks and so on until the project finishes.
Thanks,
Steve
I have a project model with a start and end date.
I want to bring back all pojects that are exactly a week old, 2 weeks, 3 weeks etc
Is there a way to do this using a scope?
Thanks in advance
Great Video!!
I would love to use this with a population density overlay.. does anyone know where I can get that data?
Posted in Should scopes be seperated out to allow chaining or be specific to the what you are trying to do?
Hi Jacob,
Thanks for your comment. You can indeed get tenders that have been assigned to the Contractor through the method above but this is just for those assigned to them directly and not indirectly through groups they belong to.
You can do this seperately via the ContractorDetail.find(contractor).tenders_by_group, I suppose I was wondering what would be the best way to combine these?
I have a method (not yet a scope) that I was playing with on the Tender model just to see if it worked.
def all_tenders
all = self.tenders
all << self.tenders_by_group
all.uniq
end
Is this the most efficient way?
Posted in Should scopes be seperated out to allow chaining or be specific to the what you are trying to do?
I am trying to write an internal tender application and am having trouble figuring out the best approach for writing a scope.
I have the following models
class Tender < ApplicationRecord
belongs_to :user
belongs_to :company_detail
has_many :questions
has_many :assets
has_many :tender_groups, dependent: :destroy
has_many :groups, through: :tender_groups
has_many :tender_contractors, dependent: :destroy
has_many :contractor_details, through: :tender_contractors
has_many :bids
has_many :contractor_details_from_groups, through: :groups, source: :contractor_details
accepts_nested_attributes_for :tender_groups
accepts_nested_attributes_for :tender_contractors
accepts_nested_attributes_for :assets
validates :title, :length_days, :summary, presence: true
scope :live, ->{ where( "tender_end_date > ?", Time.current ) }
scope :all_public, ->{ where( public: true ) }
end
class ContractorDetail < ApplicationRecord
filterrific :default_filter_params => { :sorted_by => 'business_name_asc' },
:available_filters => %w[
sorted_by
search_query
with_business_type_id
]
self.per_page = 10
has_many :users
has_many :insurances
has_many :ratings
has_many :issues
belongs_to :business_type
has_and_belongs_to_many :projects
has_one :address, as: :addressable
has_one :pre_qualify_info
has_many :references
has_many :bids
has_many :group_contractors
has_many :groups, through: :group_contractors
has_many :tender_contractors
has_many :tenders, through: :tender_contractors
has_many :tenders_by_group, through: :groups, source: :tenders
scope :search_query, lambda { |query|
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conditions = 2
where(
terms.map {
or_clauses = [
"LOWER(contractor_details.business_name) LIKE ?",
"LOWER(contractor_details.contact_email) LIKE ?"
].join(' OR ')
"(#{ or_clauses })"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conditions }.flatten
)
}
scope :sorted_by, lambda { |sort_option|
# extract the sort direction from the param value.
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^business_name_/
# Simple sort on the name colums
order("LOWER(contractor_details.business_name) #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
scope :with_business_type_id, lambda { |business_type_ids|
where(:business_type_id => [*business_type_ids])
}
def all_tenders
all = self.tenders
all << self.tenders_by_group
all.uniq
end
# This method provides select options for the `sorted_by` filter select input.
# It is called in the controller as part of `initialize_filterrific`.
def self.options_for_sorted_by
[
['Name (a-z)', 'business_name_asc'],
['Name (z-a)', 'business_name_desc'],
['Business Type (a-z)', 'business_type_name_asc']
]
end
def contact_fullname
"#{contact_first_name} #{contact_last_name}"
end
end
Some information on how tenders work:
Tenders can be public viewable by all contractors
Tenders have a end date (tender_end_date) where they are no longer visible to contractors
Contractors can be added to tenders via the tender_contractors (many to many)
Contractors can also be added to groups to then allow mass assignment to a tender by groups (tender_groups)
What I am looking at doing is writing a scope to pull back all tenders assigned to a given contractor that are live (not past the tender_end_date). The should include public tenders.
Should I seperate these out into individual scopes live I have started to? Public and Live, so that these can be used on their own or chained to give me exactly what I need.
Also how would I go about writing the scope to find the tenders assigned to the contractor, would this need to be written in sql?
If more detail is needed please let me know.
Thanks in advance for taking your time to look at this. I am still learning my craft.
Whats the best way to tackle cron jobs on Heroku as the whenever gem isn't supported?
Hi I have 2 models Tender and Group shown below.
For a Tender you can add Contractors (contractor_details) and you can also add Groups (groups). Groups also contain contractor_details.
This is so that they may want to categorize certain contractors they use alot into specific groups by speciality e.g. Joiners, Electrians e.t.c..
What would the best way for me to check all contractors added to a tender including through groups, this is so that I can then do authorization and only allow those contractors added to view the tender page?
class Group < ApplicationRecord
belongs_to :company_detail
has_many :group_contractors, dependent: :destroy
has_many :contractor_details, through: :group_contractors
has_many :tender_groups
has_many :tenders, through: :tender_groups
accepts_nested_attributes_for :group_contractors
validates :name, presence: true
end
class Tender < ApplicationRecord
belongs_to :user
belongs_to :company_detail
has_many :questions
has_many :assets
has_many :tender_groups, dependent: :destroy
has_many :groups, through: :tender_groups
has_many :tender_contractors, dependent: :destroy
has_many :contractor_details, through: :tender_contractors
has_many :bids
accepts_nested_attributes_for :tender_groups
accepts_nested_attributes_for :tender_contractors
accepts_nested_attributes_for :assets
validates :title, :length_days, :summary, presence: true
end
Hi,
I've got a system where users upload documents with expiry dates. I want to send the user an email reminder 1 month and 1 week before expiration to remind them to upload a new document.
What's the best way to do this?
Thanks in advance.
Steve
Thanks for the super speedy response :)
Hi,
I have currently two Devise models Company and Contractor.
When they sign in I want to redirect them to a dashboard page. At the moment I have a before_action on the dashboard controller that only allows authenticate_company! so when a contractor signs in they can't view the dashboard.
Anyone done something like this before? Also is there an easy way to check which user is currently logged in so that I can show them different partial views?
Thanks in advance