Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Array in model not working... where should I put it?

model array

  • Please log in to reply
3 replies to this topic

#1 newguy

newguy

    Passenger

  • Members
  • 2 posts

Posted 10 September 2013 - 04:34 AM

I'm relatively new to Rails. I'm working on an app to search craigslist (simply for personal use). I have an import method that takes a category and state or city, then loads the corresponding RSS feed(s) from craigslist.

 

Currently I have two models, Import and Post. I never really intended the Import model to have a corresponding table, but just a .rb file to store things related to the import method. I mean to have an import method load an RSS feed and create a Post for every item. Then I would be able to search Posts as usual. But I've ran into trouble...

 

My model begins with an array of every craigslist category, of every state and then all of the cities in one array.

class Import < ActiveRecord::Base
  require 'rss'
  require 'open-uri'
 
  categories = ['ccc', 'act', 'ats', 'kid', 'wrg', 'res']  # real array is substantially longer

  alabama = ['auburn', 'bham', 'dothan', 'shoals', 'gadsden', 'huntsville', 'mobile', 'montgomery', 'tuscaloosa']
  alaska = ['anchorage', 'fairbanks', 'kenai', 'juneau']
  # ... goes on for all 50 states

  states = [alabama, alaska, arizona]  # goes on for all 50
  zones = states.flatten

Then my import method begins. I start by checking that the given category exists:

def self.import(category, zone, state)
  if categories.include?(category)

But when I enter "Import.import('web', 'dallas')" in the Rails console, I get the following:

NameError: undefined local variable or method `categories' for #<Class:0x007f8c6e6e50d0>

I acknowledge that I'm probably doing all sorts of things wrong. Can anyone rip this apart and tell me how I should go about it? Where should I put these arrays if not in the model, and would it be better to persist them to the database?

 

Thanks for taking the time.



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 10 September 2013 - 05:15 AM   Best Answer

You can't have variables in global space like this. You can however have constants.

 

Try this instead

CATEGORIES = ['ccc', 'act', 'ats', 'kid', 'wrg', 'res', ...]

def self.import(category, zone, state)
  if CATEGORIES.include?(category)
    ...
  end
end

The CATEGORIES constant can be used in other files as well, by writing Import::CATEGORIES (Import being the name of the model)


Blog: http://ohm.sh | Twitter: madsohm


#3 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 10 September 2013 - 05:45 AM

How about using @ symbol to create class level instance variables?

 

@categories = ['ccc', 'ddd'....... ] 


  • Ohm likes this

#4 newguy

newguy

    Passenger

  • Members
  • 2 posts

Posted 10 September 2013 - 04:38 PM

Those both worked, thanks! Well, they got me onto the next errors at least. I'm guessing I should probably go for the constant over class level instance variables, because the categories will never change? But it's good to know this, thanks again.







Also tagged with one or more of these keywords: model, array

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users