# AR: dynamic accessors don't always work?

**URL:** <https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038>\
**Category:** Rails\
**Created:** [May 4, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038 "2010-05-04T17:50:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 4, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038/1 "2010-05-04T17:50:01Z")

</div>

This is curious. Consider ‘record’ below, derived from an SQL query.  
Notice that record.station\_id returns the station id properly, but  
record.start\_time returns nil. What’s the difference? (FWIW, I _can_  
access the start\_time via the construct record[:start\_time]).

This took me a while to track down, so I need to know: what’s the  
difference between the two fields? And - more importantly - what part of  
the documentation explains what’s going on?

> > record  
> > =\> #\<WeatherObservation precipitation\_m: 0.0\>

> > record.station\_id  
> > =\> “KCAMILLV8” # got the station id

> > record.start\_time  
> > =\> nil # where’s the start time?

> > record[:start\_time]  
> > =\> “2007-01-01 00:00:00” # ah - there it is! but wtf?

> > record.attributes  
> > =\> {“end\_time”=\>“2007-01-02 00:00:00”, “precipitation\_m”=\>0.0,  
> > “start\_time”=\>“2007-01-01 00:00:00”, “station\_id”=\>“KCAMILLV8”,  
> > “temperature\_c”=\>“8.88889”}

I’m not sure this is germane to the problem, but here’s the SQL query  
that produced the record. NOTE: neither “station\_id” nor “start\_time”  
are ‘native’ slots in the WeatherObservation class:

```
SELECT st.datetime AS start_time,
       et.datetime AS end_time,
       ws.station_id AS station_id,
       wo.temperature_avg_c AS temperature_c,
       wo.precipitation_m AS precipitation_m
  FROM weather_observations AS wo

```

INNER JOIN weather\_stations AS ws ON wo.weather\_station\_id = ws.id  
INNER JOIN time\_dimensions AS st ON wo.start\_time\_id = st.id  
INNER JOIN time\_dimensions AS et ON wo.end\_time\_id = et.id  
WHERE ws.id IN (#{station\_ids}) AND  
st.datetime BETWEEN ‘#{start\_date}’ and ‘#{end\_date}’ AND  
et.datetime BETWEEN ‘#{start\_date}’ and ‘#{end\_date}’

… all wrapped up in WeatherObservation.find\_by\_sql(), of course.

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 4, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038/2 "2010-05-04T18:45:28Z")

</div>

On May 4, 4:50 pm, Fearless F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is curious. Consider ‘record’ below, derived from an SQL query.  
> Notice that record.station\_id returns the station id properly, but  
> record.start\_time returns nil. What’s the difference? (FWIW, I _can_  
> access the start\_time via the construct record[:start\_time]).
> 
> This took me a while to track down, so I need to know: what’s the  
> difference between the two fields? And - more importantly - what part of  
> the documentation explains what’s going on?

what columns does weather\_observations have ? From glancing at the  
relevant bit of the internals i think an odd thing might happen if  
weather\_observations itself had a start\_time column of its own that  
was of a different type to the data grabbed here

Fred

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 4, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038/3 "2010-05-04T19:35:53Z")

</div>

Frederick C. wrote:

> what columns does weather\_observations have ? From glancing at the  
> relevant bit of the internals i think an odd thing might happen if  
> weather\_observations itself had a start\_time column of its own that  
> was of a different type to the data grabbed here
> 
> Fred

I had the same thought, but while WeatherObservation has a  
start\_time\_id, but it doesn’t have a start\_time slot. Is AR being  
clever about foreign key references? Here’s the schema:

create\_table “weather\_observations”, :id =\> false, :force =\> true do  
|t|  
t.integer “start\_time\_id”  
t.integer “end\_time\_id”  
t.integer “weather\_station\_id”  
t.float “temperature\_max\_c”  
t.float “temperature\_avg\_c”  
t.float “temperature\_min\_c”  
t.float “dewpoint\_max\_c”  
t.float “dewpoint\_avg\_c”  
t.float “dewpoint\_min\_c”  
t.float “humidity\_max”  
t.float “humidity\_avg”  
t.float “humidity\_min”  
t.float “pressure\_max\_kpa”  
t.float “pressure\_avg\_kpa”  
t.float “pressure\_min\_kpa”  
t.float “windspeed\_max\_mps”  
t.float “windspeed\_avg\_mps”  
t.float “gustspeed\_max\_mps”  
t.float “precipitation\_m”  
end

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 4, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038/4 "2010-05-04T19:47:31Z")

</div>

On May 4, 6:35 pm, Fearless F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I had the same thought, but while WeatherObservation has a  
> start\_time\_id, but it doesn’t have a start\_time slot. Is AR being  
> clever about foreign key references? Here’s the schema:

is there a belongs\_to :start\_time ?  
If so then it’s the accessor for that association that’s being called,  
not the attribute reader (and it’s nil because the object at hand has  
no start\_time\_id

Fred

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 4, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/ar-dynamic-accessors-dont-always-work/188038/5 "2010-05-04T20:41:57Z")

</div>

Frederick C. wrote:

> is there a belongs\_to :start\_time ?  
> If so then it’s the accessor for that association that’s being called,  
> not the attribute reader (and it’s nil because the object at hand has  
> no start\_time\_id
> 
> Fred

Give this man a cigar! That’s exactly what’s going on.

class WeatherObservation \< ActiveRecord::Base  
belongs\_to :start\_time, :class\_name =\> ‘TimeDimension’, :foreign\_key  
=\> ‘start\_time\_id’  
belongs\_to :end\_time, :class\_name =\> ‘TimeDimension’, :foreign\_key =\>  
‘end\_time\_id’  
belongs\_to :weather\_station  
…  
end

The ActiveRecord::Associations::ClassMethods doc has a nice section on  
“auto generated methods” – I didn’t notice that it’s the argument to  
:belongs\_to that defines the method name and not :foreign\_key, i.e. it  
generates:

WeatherObservation#start\_time  
rather than:  
WeatherObservation#start\_time\_id

Good catch – thanks.
