On Sat, Apr 16, 2011 at 3:05 AM, Fearless Fool <li...@ruby-forum.com> wrote:
> Hi Bryan: > > Bryan Crossland wrote in post #993150: > >> PremiseWeatherStation.create(:premise => self, :weather_station => > >> station) > > I've been unable to reproduce your error in the rails server or in the > > console. However, what I do see is that "self" is a reserved word. The > > variable name of the object you want to pass in should not be called > > "self". > > Thank you for digging around. I should have shown more context -- > 'self' is in fact what I intended: > ========= > class Premise < ActiveRecord::Base > has_many :premise_weather_stations, :dependent => :destroy > has_many :weather_stations, :through => :premise_weather_stations > ... > def find_local_weather_stations > stations = WeatherStation.find_stations_near(self) > premise_weather_stations = stations.map do |station| > PremiseWeatherStation.create(:premise => self, :weather_station => > station) > end > end > ... > end > ========= > which is to say, find_local_weather_stations is an instance method which > associates this Premise ('self') with a list of WeatherStation > ('station') objects, creating a PremiseWeatherStation object to form > each association. > > What's really odd is that I monkey patched the raise_on_type_mismatch() > method to print out the record and the @reflection. Regardless of where > I run them (console, test, or server), they print out as the same. > > HOWEVER, record.is_a?(@reflection.klass) returns true in the console or > test suite, false in the server. I'm really mystified. > > I don't know why your monkey patch is showing one thing in the console and something else in the server. However I suspect that the reason is the same as why you are getting the type mismatch in the first place. I believe it has to do with how the find_stations_near method is working. Since I don't know how you wrote that method I had to assume that what you were doing was getting a list of WeatherStation objects. If I just grab all the WeatherStation objects in my DB, regardless of if they are WeatherStation or NOAA type and run them through your map and PremiseWeatherStation.create statements they work fine. Both server and console. I ran it again but this time told it to return all NOAA objects instead of WeatherStation. It worked without issue. What is the code for the method find_stations_near? B. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.