Weird Problem With Active Record


#1

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.soen.info - Index of online software engineering knowledge
http://www.cusec.net - Canadian University Software Engineering
Conference
http://www.soenlive.com - Presentations from CUSEC


#2

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


#3

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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

John K.
http://www.kopanas.com

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


#4

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