Reputation: 18871
I am using Ruby on Rails 3.0.7 and I have an "User :has_many Articles
" Record Association for which I retrieve user's articles in this way:
@user.articles
# => [#<Article id: 1, title: "Title 1">, #<Article id: 2, title: "Title 2">, #<Article id: ..., title: "Title ...">]
However, for what I need to do, I would like to retrieve only article's id
values from that association.
BTW: It should be a more performant operation of the above.
How is it possible to do that in an "direct" way? That is, there is a Ruby on Rails method to accomplish that by keeping unchanged the :has_many
association statement for both involved models? If so, how?
Upvotes: 4
Views: 1424
Reputation: 1363
gotcha:
@user.article_ids<< new_id
does not work, it seems to be a read-only array
Upvotes: 0
Reputation: 15417
http://guides.rubyonrails.org/association_basics.html#has_many-collection_singular
@user.article_ids
Upvotes: 6