# Weird Problem With Active Record

**URL:** <https://www.ruby-forum.com/t/weird-problem-with-active-record/95720>\
**Category:** Rails\
**Created:** [June 10, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/weird-problem-with-active-record/95720 "2006-06-10T21:59:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [June 10, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/weird-problem-with-active-record/95720/1 "2006-06-10T21:59:59Z")

</div>

Is it me or is my model getting the created\_at from the wrong model!

I have a HABTM relationship between Problems and Tags. In one of my  
controller I do the following:

def view  
@tag = Tag.find\_by\_name @params[:id]  
end

In my view I do this:

```
<% @tag.problems.each do |problem| %>
	...
	<%= problem.created_at %>
	...
<% end %>

```

Now, how come the created\_at is being taken from the HABTM  
relationship instead of the problem table itself? This does not make  
sense to me. How would I get created\_at from Problem table instead  
of problems\_tag table?

Thanks for your help?

John K.  
[http://www.kopanas.com](http://www.kopanas.com)

============================================================  
[http://www.soen.info](http://www.soen.info) - Index of online software engineering knowledge  
[http://www.cusec.net](http://www.cusec.net) - Canadian University Software Engineering  
Conference  
[http://www.soenlive.com](http://www.soenlive.com) - Presentations from CUSEC

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [June 12, 2006, 3:59am UTC](https://www.ruby-forum.com/t/weird-problem-with-active-record/95720/2 "2006-06-12T03:59:07Z")

</div>

From the documentation for has\_and\_belongs\_to\_many:

Any additional fields added to the join table will be placed as  
attributes when pulling records out through has\_and\_belongs\_to\_many  
associations. Records returned from join tables with additional  
attributes will be marked as ReadOnly (because we can?t save changes  
to the additional attrbutes). It?s strongly recommended that you  
upgrade any associations with attributes to a real join model (see  
introduction).

Pete Y.  
[http://9cays.com](http://9cays.com)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [June 12, 2006, 7:45am UTC](https://www.ruby-forum.com/t/weird-problem-with-active-record/95720/3 "2006-06-12T07:45:51Z")

</div>

I am not sure this answers my question. Does that mean that  
created\_at was overode by the join\_table.created\_at and their is  
nothing I can do about that? HABTM must be more sophisticated then  
that no?

On 11-Jun-06, at 9:58 PM, Pete Y. wrote:

> Pete Y.
> 
> > ```
> > @tag = Tag.find_by_name @params[:id]
> > 
> > ```
> > 
> > Now, how come the created\_at is being taken from the HABTM
> > 
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

John K.  
[http://www.kopanas.com](http://www.kopanas.com)

============================================================  
[http://www.soen.info](http://www.soen.info) - Index of online software engineering knowledge  
[http://www.cusec.net](http://www.cusec.net) - Canadian University Software Engineering  
Conference  
[http://www.soenlive.com](http://www.soenlive.com) - Presentations from CUSEC

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [June 12, 2006, 9:01am UTC](https://www.ruby-forum.com/t/weird-problem-with-active-record/95720/4 "2006-06-12T09:01:59Z")

</div>

On 12/06/2006, at 3:45 PM, John K. wrote:

> I am not sure this answers my question. Does that mean that  
> created\_at was overode by the join\_table.created\_at and their is  
> nothing I can do about that?

Yes. Any attributes on a join table appear in records you get when  
querying a HABTM relationship that uses that join table.

> HABTM must be more sophisticated then that no?

HABTM is not more sophisticated than that, which is why “It?s  
strongly recommended that you upgrade any associations with  
attributes to a real join model”. See the section on “Association  
Join Models” in the ActiveRecord::Associations::ClassMethods  
documentation.

Pete Y.  
[http://9cays.com](http://9cays.com)
