JM

Joined

4,360 Experience
27 Lessons Completed
0 Questions Solved

Activity

That's exactly what I want but strangely r bough. Can't access the def admin method when I try to hide sine links the agents aren't supposed to see..... I can't find anything wrong with my code.... Or is there anything working currentky

Hello there I am in a little fix and cant figure out whats going in, i am attempting to have a devise model User share via STI with 2 other sub-classes . I am able to populate both classes with data via console and I can query the user table and get data from the 2 sub-classes, but if i define a custom method in User.rb and trying to access via current_user its not posibble see code

 class User < ActiveRecord::Base
    scope :agent, -> { where(type: "Agent") }
    scope :team_leader, -> { where(type: "TeamLeader") }
     devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
    has_many :attendances
    belongs_to :team

     self.inheritance_column = :type

    def self.types
      %w(Agent TeamLeader)
    end

     def admin?
        type == "TeamLeader"
      end

  end


  class TeamLeader < User

  end


  class Agent < User

   end

so when i try to hide some resource from the type Agent I get an error of undefined method admin?

Posted in Performing calculations using scopes

Awesome works like magic .. and when referring to the model its-self i can just use the keyword self !!!

Chris You Rock \m/ :)

Posted in Performing calculations using scopes

got yah !

the if its a class method in attendance.rb the it should read like this ?

  def self.planned_shrinkage
       (planned_leave.count + maternity_leave.count + paternal_leave.count)  / present.count.to_f * 100
   end

    no attendaces.planned_leave. count but planned leave .count 

Posted in Performing calculations using scopes

That is fine, I am do so currently , my question was where will the custom method be ? same class as scope or elsewhere

  def ps
       (attendances.planned_leave.count + b.attendances.maternity_leave.count)  / b.attendances.count.to_f * 100
  end

 #@attendance_sheet.attendaces.ps doesnt work 

I am assuming is in the attendance.rb .... then how do i call it form attendance_sheet view

Posted in Performing calculations using scopes

Hi Chris,

The above scopes are in my attendance.rb but i would wish the method to be in attendance_sheet.rb , rem

  class AttendanceSheet < ActiveRecord::Base
    has_many :attendances, dependent: :destroy
    accepts_nested_attributes_for :attendances, reject_if: :all_blank, allow_destroy: true
    belongs_to :team
 end

is it even possible to call that method from outside attendance_sheet while the scopes live in attendance ?

cheers

Posted in Performing calculations using scopes

super awesome thanks

Posted in Pre-populate association for nested form

So finally i ended up with this :

 def create
     @attendance_sheet = AttendanceSheet.new(attendance_sheet_params)
     @team = Team.find(attendance_sheet_params[:team_id])
     @team.users.each do |u|
       @attendance_sheet.attendances .build(user: u)
     end

     respond_to do |format|
       if @attendance_sheet.save
         format.html { render :edit }
         format.json { render :show, status: :created, location: @attendance_sheet }
       else
         format.html { render :new }
         format.json { render json: @attendance_sheet.errors, status: :unprocessable_entity }
       end
     end
   end

and it works like magic .... cheers previously it was working but i eliminated the shovel :)

Posted in Pre-populate association for nested form

Hi so here is what i have :

class Attendance < ActiveRecord::Base
  belongs_to :attendance_sheet
  belongs_to :user
end

class AttendanceSheet < ActiveRecord::Base
  has_many :attendances, dependent: :destroy
  accepts_nested_attributes_for :attendances, reject_if: :all_blank, allow_destroy: true
  belongs_to :team
end

class User < ActiveRecord::Base
  has_many  :attendances, dependent: :destroy
  belongs_to :admin
end

class Team < ActiveRecord::Base
  has_many :users
  belongs_to :admin
  has_many :attendance_sheets
end

As you can see the attendances are nested in attendance_sheet , so when a new attendance sheet is created it has a team assigned to it depending on the user who is logged in the user had to click "Add" to populate the needed number of attendances of users for that day .

I am looking for a way since the team is already selected can all the names of the users in that team be populated all at once , current my attendance new looks like this

from an old rails version, and doesnt work

def new
    @attendance_sheet = AttendanceSheet.new
    @team = @attendance_sheet.team
    @team.users.each do |u|
      @attendance_sheet.attendances << Attendance.new(:user => u)
    end
  end

alternatively can i use somethings like

    4.times { @attendance_sheet.attendances.build }

where 4 is passed dynamically as the size of the team ?

Your help or guidance is appreciated cheers

Posted in Performing calculations using scopes

hello , hopefully this isn’t too ambitious but i am hoping there is a way to do simple summation using defined scopes example is i have these scopes :

      scope :present, -> { where(present: true) }
      scope :absent, -> { where(present: false) }
      scope :emergency_leave, -> { where(present: false) && where(reason: "Emergency Leave") }
      scope :sick_leave, -> { where(present: false) && where(reason: "Sick Leave") }
      scope :planned_leave, -> { where(present: false) && where(reason: "Planned Leave") }

is it possible to define a method within the same class that can take something like
def x
absent.count + emergency_leave.count ?
end

how would you re-use the scopes to do calculations ?

Posted in validate presence of specific domain in email

Finally worked , the issue was separating "@" and the "domain.org" part i used
+@+mydomain.org

Started GET "/attendance_sheets/new" for 127.0.0.1 at 2015-12-11 18:03:36 +0300
Processing by AttendanceSheetsController#new as HTML
  Rendered attendance_sheets/_form.html.erb (3.3ms)
  Rendered attendance_sheets/new.html.erb within layouts/application (4.0ms)
Completed 500 Internal Server Error in 25ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined method `map' for nil:NilClass):
    16:           <% end %>
    17:           <div class="field">
    18:             <%= f.label :Team %><br>
    19:             <%= f.collection_select(:team_id, @team, :id, :team_code) %>
    20:           </div>
    21:           <div class="field">
    22:             <%= f.label :date %><br>
  app/views/attendance_sheets/_form.html.erb:19:in `block in _app_views_attendance_sheets__form_html_erb___3904306276885524695_69942133851520'
  app/views/attendance_sheets/_form.html.erb:5:in `_app_views_attendance_sheets__form_html_erb___3904306276885524695_69942133851520'
  app/views/attendance_sheets/new.html.erb:7:in `_app_views_attendance_sheets_new_html_erb__3506630663733362946_69942134392600'

will that help ?

Hi Chris,

the first part @team is an array the Teams assigned to a specific Team leader

Posted in validate presence of specific domain in email

I used "mydomain" just as an example ... i had replaced all that with the specific domain.

will try with @ and the domain separated

Hi,

I have been struggling with this error for the last 3 hours and its s bit frustrating: Here is the scenario

I am trying to populate a select box with Team names that belong to a specific team leader here are my models and code

user.rb
belongs_to :team
  has_many  :attendances, dependent: :destroy
  belongs_to :team_leader, class_name: "User"
  has_many :agents, class_name: "User",
                    foreign_key: "team_leader_id"

team.rb
class Team < ActiveRecord::Base

  validates :team_code, :description, presence: true
  validates :team_code, uniqueness: true

  has_and_belongs_to_many :users
  has_many :attendance_sheets
end


attendance_sheet/new

<div class="field">
            <%= f.label :Team %><br>
            <%= f.collection_select(:team_id, @team, :id, :team_code) %>
          </div>

the other partial with the user, _attendance_fields.html.erb

<div class="nested-fields">
    <tr>
       <td><%= f.collection_select(:user_id, @users, :id, :name ) %></td>
       <td><%= f.check_box :present%></td>
       <td><%= f.select :reason, ["a","b"] %></td>
       <td><%= link_to_remove_association "Remove attendance", f, class: "form-button btn btn-default" %></td>
    </tr>
</div>

whenever i try to create an attendance sheet i the undefined method map error for both the partial and main form. I want to be able to see on teams that belong the user logged and also only the users belonging to the team/logged team_leader

I am not sure if my self join table is well done either

appreciated

Posted in validate presence of specific domain in email

Hi there, is a way via regex to validate an email and make sure it belongs to a specific domain, I found the below regex on SO and it doesnt seem to do the job.

validates :email, format: { with: /\b[A-Z0-9._%a-z\-]+@mydomain\.org\z/, message: "must be a mydomain.com account" }

Posted in Nested forms with rows like a table

Hi Chris the table worked brilliantly.. but i am still struggling with automatically populating the nested form with related record.. is it even possible in the first place ? such that if one selects a team drop down and click add attendance a specific number of rows are added with the team members names of the persons in that team and all is left is to check absent or present

Posted in custom method in class to perfom simple calculation

Hi James,

I have just tweaked it a little bit and it has worked thanks so much for the scoping idea.

Much appreciated !!! yessssss

Posted in custom method in class to perfom simple calculation

thanks James, allow me to engage you further for clarity

scoping sounds like a good idea.. but my problem occurs when i try to call AttendanceSheet.find(0)..... how exactly is the syntax ?

i tried that previously and i kept running into ...no Attendace_sheet with 'id' = :id ..... where is it best to call that line ?, show controller or the show view ...

much appreciated

Posted in custom method in class to perfom simple calculation

Hi Chris,

Checkout mt simple scenario below: I am trying to get the total count of all absent and present person from an attendance sheet here are my models.

 class AttendanceSheet < ActiveRecord::Base
    attr_accessor :totalp, :totala
  has_many :attendances
  accepts_nested_attributes_for :attendances, reject_if: :all_blank, allow_destroy: true
  belongs_to :team


  def total_present_agents
    totalp=AttendanceSheet.find(need to pass id here).attendances.where(present: true).count
  end

  def total_absent_agent
    totala=AttendanceSheet.find(need to pass id here).attendances.where(present: false).count
  end

end

The summary is to be displayed on the show page of the attendance sheet , in the show view i have this to display buy it runs without errors but displays nothing

<h2>Attendance statistics</h2>

<h5>Total present Agents <%= @attendance_sheet.totalp %></h5>
<h5>Total absent Agents <%= @attendance_sheet.totala %></h5>

if pass the id hardocoded like this:
totala=AttendanceSheet.find(8).attendances.where(present: false).count
i get what i want but of course thats not ideal at all as i want to dynamically pass the ids

so my questions is do i have to store that value in the database as i am not currently ?, also is the method in my class well defined ... what i am i missing ? Do i need this

attr_accessor :totalp, :totala

my attendance model also looks like this if that will clear thing up a bit

class Attendance < ActiveRecord::Base
  belongs_to :attendance_sheet
  belongs_to :user 
end

I have a feeling its a simple fundamental convention i am breaking , would be glad if you point me towards that direction