Save 36% for Black Friday! Learn more

Stephen Sizer

Joined

2,920 Experience
10 Lessons Completed
1 Question Solved

Activity

I have an app with Devise Users and they can sign up. Once they are signed in I want them to complete there profile before they can use the app.

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

I was thinking of having a page before delete that would populate a list of users to transfer the data to and then on that action it would delete the user :)

Posted in How do I transfer user data on delete

To give more context:

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

I have a rails app with company users using devise. I want to be able to delete users and select an employee to transfer the foreign keys to.

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

Posted in How to use Google Maps and Markers Discussion

Great Video!!

I would love to use this with a population density overlay.. does anyone know where I can get that data?

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?

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?

Posted in Nested has many through... Or is there a better way?

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

Posted in Multiple Devise Users sharing a dashboard

Thanks for the super speedy response :)

Posted in Multiple Devise Users sharing a dashboard

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

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.