Forum: Ruby on Rails Weird Problem With Active Record

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3b985461018c5edb36619bc30a70548d?d=identicon&s=25 John Kopanas (Guest)
on 2006-06-10 21:59
(Received via mailing list)
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 Kopanas
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
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-06-12 03:59
(Received via mailing list)
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 Yandell
http://9cays.com
3b985461018c5edb36619bc30a70548d?d=identicon&s=25 John Kopanas (Guest)
on 2006-06-12 07:45
(Received via mailing list)
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 Yandell wrote:

> Pete Yandell
>>     @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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


John Kopanas
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
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-06-12 09:01
(Received via mailing list)
On 12/06/2006, at 3:45 PM, John Kopanas 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 Yandell
http://9cays.com
This topic is locked and can not be replied to.