9

I have the following mongoid model that inherits from the Entry model:

class Entry::Twitter < Entry

  field :retweet_count, :type => Integer, :default => 0
  field :retweeted, :type => Boolean, :default => false
  field :favorited, :type => Boolean, :default => false

  # in_reply_to_screen_name, in_reply_to_status_id_str, in_reply_to_user_id_str
  field :reply, :type => Hash

  field :from, :type => Hash # user: id_str, name, screen_name
  field :time, :type => Time # created_at
  field :data, :type => Hash # entities (hashtags and user_mentions)
  field :assets, :type => Hash # urls from original entities
  field :service, :type => String, :default => "twitter"

  attr_accessible :assets

  # validations
  validates_presence_of :retweet_count, :from, :time, :data

  # override set_service cause of https://github.com/sferik/twitter/issues/303

  def set_service
    self.service = "twitter"
  end
end

When i try to reference it i get the following warning:

ruby-1.9.3-p125 :001 > Entry::Twitter
(irb):1: warning: toplevel constant Twitter referenced by Entry::Twitter
=> Twitter

Instead of referencing to my model it references to the Top Level Constant Twitter that is defined by a gem.

What can i do to fix this? I don't want to use another name for my class.

6
  • Which gem do you mean exaclty? this one: github.com/sferik/twitter? I have a similar problem, and loading error..
    – Matthias
    Aug 13, 2012 at 10:27
  • Your problem is related to how Ruby handles scoping. Here's a pretty good description of what's happening: stackoverflow.com/a/6282245/279024
    – rubiii
    Aug 13, 2012 at 11:32
  • @rubiii I already have seen this. But i dont know how to apply this to my specific problem. Aug 13, 2012 at 11:48
  • 1
    @Mattherick As the problem is not exactly in your code, but in the gem's code referencing the Twitter constant, I would just rename your class to TwitterEntry.
    – rubiii
    Aug 13, 2012 at 12:41
  • not the same but: Entry::Twitter == ::Twitter => true Entry::Twitter == Twitter => true
    – Betty St
    Aug 13, 2012 at 13:48

1 Answer 1

5

here is the solution: https://github.com/rails/rails/issues/6931

I just added require_dependency 'entry/twitter' to every files that references Entry::Twitter to avoid this problem, and it works fine now.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.