Forum: Ruby on Rails ActiveRecord:

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.
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2006-01-02 19:48
In the Agile Rails book, on page 232 (PDF, 4th edition) there is an
example of (within ActiveRecord) marking an article as read by a user at
the present time. Short example code (from the book) here:

	class User < ActiveRecord::Base
	  has_and_belongs_to_many :articles

	  def read_article(article)
	    articles.push_with_attributes(article, :read_at => Time.now)
	  end

	  # ...
	end

However, it seems this piece of code would only work the first time you
read a specific article. It appears to always create a new join table
post, and not to just update if there is an existing post. How would one
best solve this?
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 Duane Johnson (Guest)
on 2006-01-03 15:05
(Received via mailing list)
On Jan 2, 2006, at 11:48 AM, Henrik wrote:

> 	  end
>
It looks like the intent is to create some sort of 'log' of each
visit.  However, if this is not the intent, then you could
(potentially) use "articles.clear" before calling
articles.push_with_attributes.  Unfortunately, destroying *all*
associated articles may not be the desired result, either.

If you just want to delete articles of a certain ID you can use
articles.delete(article), and then call push_with_attributes.

Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2006-01-04 00:31
That last (articles.delete) would do the trick. Thanks! I was hoping for
a "cleaner" way to do it, ideally something like (pseudo-Ruby)
articles.push_or_update_with_attributes, but I suppose this is as good
as it gets.

Duane Johnson wrote:
> On Jan 2, 2006, at 11:48 AM, Henrik wrote:
>
>> 	  end
>>
> It looks like the intent is to create some sort of 'log' of each
> visit.  However, if this is not the intent, then you could
> (potentially) use "articles.clear" before calling
> articles.push_with_attributes.  Unfortunately, destroying *all*
> associated articles may not be the desired result, either.
>
> If you just want to delete articles of a certain ID you can use
> articles.delete(article), and then call push_with_attributes.
>
> Duane Johnson
> (canadaduane)
> http://blog.inquirylabs.com/
This topic is locked and can not be replied to.