Ruby on Rails
Monday, February 4, 2019
I don't think this is supported out-of-the-box in Rails but I did see this extension a while back that appears to add what you are looking for:
https://github.com/marshall-lee/has_array_of
Haven't personally used it but looks promising.
Eric
On Wednesday, April 11, 2018 at 7:51:00 AM UTC-4, Carlos Ferreira da Silva wrote:
I'm trying to use an array column to crete an association between 2 models. I remember that in the past this was working, but I think something has changed and now it's not working anymore. Here is the deal:Ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]Rails 5.1.5app/models/user.rb
# Having the columns id, name, email
class User < ApplicationRecord
endapp/models/group.rb
# Having the columns id, name, user_ids[]
class Group < ApplicationRecord
has_many :users, primary_key: :user_ids, foreign_key: :id
endAnd the oddest thing about this is that the following works fine:
Group.first.usersIt gives me the list of the users with the ids filled on that field, but the following doesn't work:
Group.first.users.load
It gives me an empty list. And, since the load method doesn't work, I cannot do any operation like #each or #to_a.I already did some dig up, and found the source of the problem here:
https://github.com/rails/rails/blob/v5.1.5/ activerecord/lib/active_ record/associations/ association_scope.rb#L79 The first command don't replace the original value by a Replaceable, the second does. Maybe we just need a ReplaceableArray or something like that, or I'm missing something here.Can anyone help me with that?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1bf2d946-5602-4337-acd3-f19f7460285a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment