Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading ISSUE in rails
    primarykey
    data
    text
    <p>I'm facing a problem with Loading a Constant in Rails console (rails console). Here how my structure look like this </p> <pre><code>- app - controllers - models - earning - daily_earning.rb - monthly_earning.rb - weekly_earning.rb - yearly_earning.rb - views </code></pre> <p>Some more information </p> <p>I also have a rake which look like this </p> <pre><code>namespace :past_days do desc "Past 7 Days Earning" task :earning =&gt; :environment do puts $:.select { |i| i=~ /models/ }.to_yaml 7.downto(1).each do |i| start_date = i.days.ago.beginning_of_day puts "====== Dumping past #{start_date.strftime('%A')} earning =====" end_date = start_date.end_of_day Performer.top_daily_earners(start_date,end_date) puts "====== Dumped #{start_date.strftime('%A')} earning !!! =======" puts end end end </code></pre> <p>And the <code>top_daily_earners</code> method look like this If you check this <code>@klass = DailyEarning</code> </p> <pre><code>def top_daily_earners(start_date=nil,end_date=nil) unless start_date or end_date date = 1.day.ago @start_date,@end_date = date.beginning_of_day,date.end_of_day end if start_date and end_date @start_date,@end_date = start_date,end_date end @klass = DailyEarning @earning_performers = retrieve_earnings puts "COUNT -----" puts @earning_performers.count puts "" store_earning end </code></pre> <h3>Question :</h3> <p>Now when I run rake task <code>bundle exec rake past_days:earning</code> (Rake run without any error) all work fine but when I run this </p> <p><code>rails console</code> see attach screenshot </p> <p><img src="https://i.stack.imgur.com/iqXzv.png" alt="enter image description here"></p> <p>I get errors <code>NameError: uninitialized constant DailyEarning</code> and I have manually require the file as can be seen the above screenshot </p> <p>So the POINT of all the above <strong>question</strong> is why the error on <code>rails console</code> <strong>(NameError: uninitialized constant DailyEarning)</strong> and why not the error in rake task </p> <h1>Attaching DailyEarning Code based on @dtt comment</h1> <pre><code>puts 'DailyEarning' class DailyEarning include Mongoid::Document store_in session: "writeable" field :performer_id, :type =&gt; Integer field :user_id,:type =&gt; Integer field :stage_name,:type =&gt; String field :full_name,:type =&gt; String field :start_date,:type =&gt; DateTime field :end_date,:type =&gt; DateTime field :amount,:type =&gt; BigDecimal before_create :other_details ## Please avoid using default scope because it AFAIK it make the date parameter as static class &lt;&lt; self def default_scoping where(:start_date.gte =&gt; 1.day.ago.beginning_of_day).and(:end_date.lte =&gt; 1.day.ago.end_of_day) end end private def other_details ## Fetch from Mongo Instead of Mysql to avoid the slow sql query performer_source = PerformerSource.where(performer_id: performer_id).only([:stage_name,:user_id]).first self.user_id = performer_source.user_id self.stage_name = self.stage_name #self.full_name = self.full_name end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload