Delete all associations from duplicated object - ruby-on-rails

When duplicating an object with object_dup = object.dup, all associations are copied.
object_dup.foos == object.foos
I would like to duplicate/clone object without its associations, or delete the associations all after duplication. I would like to destroy all duplicated associations on object_dup. It might be easier to just create a new object, but duplication saves me from property-setting-hell.
Is that possible?

Actually .dup method didn't duplicate associations, it just copy the foreign key (parents).
Examples:
# Original
my_post = Post.first
=> #<Post id: 1, title: 'blabla', category_id: 10>
# Duplicate
my_post.dup
=> #<Post id: nil, title: 'blabla', category_id: 10>
# Have the same category_id (10)
My best way to duplicate without some attributes :
Post.new(my_post.attributes.slice('titles'))
=> #<Post id: nil, title: 'blabla', category_id: nil>
Here we creating a new empty Post, get original post attributes with my_post.attributes and slice only attributes we want with slice('title') (accept multiples attributes, examples: slice('title', 'content', 'tags'))
.dup Documentation
.slice Documentation

Related

has_many with unsaved default in collection

I thought it would be a simple thing, but unfortunately I can't find a way to create a collection with a default entry.
There is the model Entry which can be filtered by one or more filter_entries.
There should be a fallback, when there is no association. Cause I dont't want to create a default association in the database for 100000s of entries.
Currently I made an array, which returns a default entry.
class Entry
belongs_to :object
has_many :entry_filters, dependent: :destroy
def entry_filters
super.presence || [EntryFilter.new(filter: default_filter)]
end
private
def default_entry
object.filters.find_by(default: true)
end
end
An entry with an association
Entry.first.entry_filters
=> [#<EntryFilter:0x00007f4b39cb16d8
id: 1,
entry_type: "Entry",
entry_id: 1,
filter_id: 1>]
Entry.first.entry_filters.class
=> EntrySegment::ActiveRecord_Associations_CollectionProxy
An entry without an association
Entry.second.entry_filters
=> [#<EntryFilter:0x000055e9060ac608
id: nil,
entry_type: nil,
entry_id: nil,
filter_id: 1>]
Entry.second.entry_filters.class
=> Array
It works, but I want to work with a collection, like when there is a real association linked to the entry.
Is there a way, using the ActiveRecord::Associations::HasManyAssociation to fake that collection?
I've tried several methods, but each will create thaht association instead of building it.
Entry.second.association(:entry_filters).writer([EntryFilter.new(filter: Filter.first)])
=> [#<EntryFilter:0x00007f90d0b38d88 id: 2, entry_type: "Entry", entry_id: 2, filter_id: 1>]
Entry.second.association(:entry_filters).replace([EntryFilter.new(filter: Filter.first)])
=> [#<EntryFilter:0x00007f90d0a457c8 id: 3, entry_type: "Entry", entry_id: 2, filter_id: 1>]
Entry.second.association(:entry_filters).concat([EntryFilter.new(filter: Filter.first)])
=> [#<EntryFilter:0x00007f90d0a637a5 id: 4, entry_type: "Entry", entry_id: 2, filter_id: 1>]
You can use the .build collection method to do what you want:
def entry_filters
filters = super
filters.build(filter: default_filter) if filters.blank?
filters
end
The .build method instantiates a new associated object (without saving), sets the foreign key, and adds it to the object's collection in memory. In ordinary usage you would call it like <entry_obj>.entry_filters.build(filter: ...). But, you can't do that inside .entry_filters itself or you end up with infinite recursion. Hence, we call super first to get the collection and then call .build if the collection is currently empty.
> Entry.new.entry_filters
=> #<ActiveRecord::Associations::CollectionProxy [#<EntryFilter id: nil,
filter_id: ... ]
When you call .save on the parent entry, it will also save the new entry filter at that point.

Rails/ActiveRecord has_many through: association on unsaved objects

Let's work with these classes:
class User < ActiveRecord::Base
has_many :project_participations
has_many :projects, through: :project_participations, inverse_of: :users
end
class ProjectParticipation < ActiveRecord::Base
belongs_to :user
belongs_to :project
enum role: { member: 0, manager: 1 }
end
class Project < ActiveRecord::Base
has_many :project_participations
has_many :users, through: :project_participations, inverse_of: :projects
end
A user can participate in many projects with a role as a member or a manager. The connecting model is called ProjectParticipation.
I now have a problem using the associations on unsaved objects. The following commands work like I think they should work:
# first example
u = User.new
p = Project.new
u.projects << p
u.projects
=> #<ActiveRecord::Associations::CollectionProxy [#<Project id: nil>]>
u.project_participations
=> #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil>]>
So far so good - AR created the ProjectParticipation by itself and I can access the projects of a user with u.projects.
But it does not work if I create the ProjectParticipation by myself:
# second example
u = User.new
pp = ProjectParticipation.new
p = Project.new
pp.project = p # assign project to project_participation
u.project_participations << pp # assign project_participation to user
u.project_participations
=> #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil>]>
u.projects
=> #<ActiveRecord::Associations::CollectionProxy []>
Why are the projects empty? I cannot access the projects by u.projects like before.
But if I go through the participations directly, the project shows up:
u.project_participations.map(&:project)
=> [#<Project id: nil>]
Shouldn't it work like the first example directly: u.projects returning me all projects not depending on whether I create the join object by myself or not? Or how can I make AR aware of this?
Short answer: No, second example won't work the way it worked in first example. You must use first example's way of creating intermediate associations directly with user and project objects.
Long answer:
Before we start, we should know how has_many :through is being handled in ActiveRecord::Base. So, let's start with has_many(name, scope = nil, options = {}, &extension) method which calls its association builder here, at the end of method the returned reflection and then add reflection to a hash as a cache with key-value pair here.
Now question is, how do these associations gets activated?!?!
It's because of association(name) method. Which calls association_class method, which actually calls and return this constant: Associations::HasManyThroughAssociation, that makes this line to autoload active_record/associations/has_many_through_association.rb and instantiate its instance here. This is where owner and reflection are saved when the association is being created and in the next reset method is being called which gets invoked in the subclass ActiveRecord::Associations::CollectionAssociation here.
Why this reset call was important? Because, it sets #target as an array. This #target is the array where all associated objects are stored when you make a query and then used as cache when you reuse it in your code instead of making a new query. That's why calling user.projects(where user and projects persists in db, i.e. calling: user = User.find(1) and then user.projects) will make a db query and calling it again won't.
So, when you make a reader call on an association, e.g.: user.projects, it invokes the collectionProxy, before populating the #target from load_target.
This is barely scratching the surface. But, you get the idea how associations are being build using builders(which creates different reflection based on the condition) and creates proxies for reading data in the target variable.
tl;dr
The difference between your first and second examples is the way their association builders are being invoked for creating associations' reflection(based on macro), proxy and target instance variables.
First example:
u = User.new
p = Project.new
u.projects << p
u.association(:projects)
#=> ActiveRecord::Associations::HasManyThroughAssociation object
#=> #proxy = #<ActiveRecord::Associations::CollectionProxy [#<Project id: nil, name: nil, created_at: nil, updated_at: nil>]>
#=> #target = [#<Project id: nil, name: nil, created_at: nil, updated_at: nil>]
u.association(:project_participations)
#=> ActiveRecord::Associations::HasManyAssociation object
#=> #proxy = #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil, created_at: nil, updated_at: nil>]>
#=> #target = [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil, created_at: nil, updated_at: nil>]
u.project_participations.first.association(:project)
#=> ActiveRecord::Associations::BelongsToAssociation object
#=> #target = #<Project id: nil, name: nil, created_at: nil, updated_at: nil>
Second example:
u = User.new
pp = ProjectParticipation.new
p = Project.new
pp.project = p # assign project to project_participation
u.project_participations << pp # assign project_participation to user
u.association(:projects)
#=> ActiveRecord::Associations::HasManyThroughAssociation object
#=> #proxy = nil
#=> #target = []
u.association(:project_participations)
#=> ActiveRecord::Associations::HasManyAssociation object
#=> #proxy = #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil, created_at: nil, updated_at: nil>
#=> #target = [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil, created_at: nil, updated_at: nil>]
u.project_participations.first.association(:project)
#=> ActiveRecord::Associations::BelongsToAssociation object
#=> #target = #<Project id: nil, name: nil, created_at: nil, updated_at: nil>
There's no proxy for BelongsToAssociation, it has just target and owner.
However, if you are really inclined to make your second example work, you will just have to do this:
u.association(:projects).instance_variable_set('#target', [p])
And now:
u.projects
#=> #<ActiveRecord::Associations::CollectionProxy [#<Project id: nil, name: nil, created_at: nil, updated_at: nil>]>
In my opinion which is a very bad way of creating/saving associations. So, stick with the first example itself.
This is more of a rails structure thing at the level of the ruby data structures.
To simplify it lets put it this way.
First of all imagine User as a data structure contains:
project_participations Array
projects Array
And Project
users Array
project_participations Array
Now when you mark a relation to be :through another (user.projects through user.project_participations)
Rails implies that when you add a record to that first relation (user.projects) it will have to create another one in the second realation (user.project_participations) that is all the effect of the 'through' hook
So in this case,
user.projects << project
#will proc the 'through'
#user.project_participations << new_entry
Keep in mind that the project.users is still not updated because its a completely different data structure and you have no reference to it.
So lets take a look what will happen with the second example
u.project_participations << pp
#this has nothing hooked to it so it operates like a normal array
So In conclusion, this acts like a one way binding on a ruby data structure level and whenever you save and refresh your objects, this will behave the way you wanted.
At the risk of some serious oversimplification let me try to explain what is going on
What Most of the other answers are trying to tell you is that these objects have not been linked yet by active record until they are persisted in the DB. Consequently the association behavior that you are expecting is not fully wired up.
Notice that this line from your first example
u.project_participations
=> #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil>]>
Is identical to the result from your second example
u.project_participations
=> #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil>]>
This statement from your analysis of what you think rails is doing is inaccurate:
So far so good - AR created the ProjectParticipation by itself and I
can access the projects of a user with u.projects.
AR record has not created the ProjectParticipation. You have declared this relationship in your model. AR is just returning proxy for the collection that it will have at some point in the future, which when populated assigned, etc, you will be be able to iterate over and query its members etc.
The reason that this works:
u.projects << p
u.projects
=> #<ActiveRecord::Associations::CollectionProxy [#<Project id: nil>]>
But this doesn't
pp.project = p # assign project to project_participation
u.project_participations << pp # assign project_participation to user
u.project_participations
=> #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, user_id: nil, project_id: nil, role: nil>]>
u.projects
=> #<ActiveRecord::Associations::CollectionProxy []>
Is that in the first case you are just adding objects to an array that your user instance has direct access to. In the second example the has_many_through relationship reflects a relationship that happens at the database level. In the second example in order for the your projects to be accessible through your user, AR has to actually run a query that joins the tables and returns the data you are looking for. Since none of these objects is persisted yet that database query can't happen yet so all you get back are the proxies.
The last bit of code is misleading because it is not actually doing what you think.
u.project_participations.map(&:project)
=> [#<Project id: nil>]
In this case you have a user which is directly holding an array of ProjectParticipations one of which is directly holding a project so it works. It is not actually using the has_many_through mechanism in they way you think.
Again this is a bit of an oversimplification but that is the general idea.
Associations are defined on database level and make use of database table's primary key (and in polymorphic cases, class name). In case of has_many :through the lookup on association (say, User's Projects) is:
Fetch all User-Project pairs, whose user_id is a certain value (primary key of an existing User in the database)
Fetch all project_id (primary keys of projects) from these pairs
Fetch all Projects by resulting keys
Of course, these are simple terms, in database terms it's much shorter and uses more complicated abstractions, such as an inner join, but the essence is the same.
When you create a new object via new, it is not yet saved in the database, and therefore has no primary key (it's nil). That said, if the object is not in a database yet, you have no way of referencing it from any ActiveRecord's association.
Side note:
There is a possibility, however, that a newly created (and not saved yet) object will act as if something is associated with it: it might show entries belonging to NULL. This usually means you have an error in your database schema that allows such things to happen, but hypothetically, one could design his database to make use of this.

extract attributes values from an activerecord query object in rails 2.3

In my controller I have a #attach object and when I inspect it, it has values as
[#<MessageAddlAttachment id: 80, reminder_id: 112, msg_attachment_file_name: "24.png", msg_attachment_content_type: "image/png", msg_attachment_file_size: 272368, created_at: "2013-10-10 12:04:37", updated_at: "2013-10-10 12:04:37">, #<MessageAddlAttachment id: 81, reminder_id: 112, msg_attachment_file_name: "37.png", msg_attachment_content_type: "image/png", msg_attachment_file_size: 333986, created_at: "2013-10-10 12:04:37", updated_at: "2013-10-10 12:04:37">]
So now after some operation I need to create an entry in this MessageAddlAttachment table with different ids. How can I achieve it. I tried dup but it will have same ids. Please help
dup is your friend in rails 4. it will create a copy but removes the id value:
u = User.first
=> #<User id: 1, ...>
u.dup
=> #<User id: nil, ...>
u.dup.save
(0.2ms) begin transaction
...
Starting either rails 3.2 or 3.1, you want to use dup. Prior to that, you should use clone instead. That will give you new values for the id field; you might want to pay attention to what happens to your created_at and updated_at fields as well.
Another issue to watch out for is if you have any date fields with validations that say they must be after "today's date", they may have been valid when the original record was saved, but not when the new record is saved. How you resolve this will depend on your situation; you might want to disable validations completely while cloning, or adjust the values in the new records.

Deleting records from HABTM association

I'm trying to do something fairly simple. I have two models, User and Group. For simplicity's sake, let's say they look like this:
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end
and
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
Now, for some reason, I have a user that has the same group twice. In the Rails Console:
user = User.find(1000)
=> #<User id: 1000, first_name: "John", last_name: "Doe", active: true, created_at:
"2013-01-02 16:52:36", updated_at: "2013-06-17 16:21:09">
groups = user.groups
=> [#<Group id: 1, name: "student", is_active: true, created_at: "2012-12-24 15:08:59",
updated_at: "2012-12-24 15:08:59">, #<Group id: 1, name: "student", is_active: true,
created_at: "2012-12-24 15:08:59", updated_at: "2012-12-24 15:08:59">]
user.groups = groups.uniq
=> [#<Group id: 1, name: "student", is_active: true, created_at: "2012-12-24 15:08:59",
updated_at: "2012-12-24 15:08:59">]
user.save
=> true
And there is some SQL output that I've silenced. I would think that everything should be all set, but it's not. The groups aren't updated, and that user still has both. I could go into the join table and manually remove the duplicates, but that seems cludgy and gross and unnecessary. What am I doing wrong here?
I'm running Rails 3.2.11 and Ruby 1.9.3p392
Additional note: I've tried this many different ways, including using user.update_attributes, and using group_ids instead of the groups themselves, to no avail.
The reason this doesn't work is because ActiveRecord isn't handling the invalid state of duplicates in the habtm association (or any CollectionAssociation for that matter). Any ids not included in the newly assigned array are deleted - but there aren't any in this case. The relevant code:
# From lib/active_record/associations/collection_association.rb
def replace_records(new_target, original_target)
delete(target - new_target)
unless concat(new_target - target)
#target = original_target
raise RecordNotSaved, "Failed to replace #{reflection.name} because one or more of the " \
"new records could not be saved."
end
target
end
The 'targets' being passed around are Arrays of assigned records. Note the call to delete(target - new_target) is equivalent in your case to delete(user.groups - user.groups.uniq) which results in an empty Array passed (since comparison is based on the id attribute of each record).
Instead, you'll need to clear out the association and then reassign the single group again:
group = user.groups.first
user.groups.clear
user.groups << group
This might be a way to cleanup those duplicates (it handles any number of groups of duplicate associations):
user = User.find(1000)
user.groups << user.groups.group_by(&:id).values.find_all {|v| v.size > 1}.each {|duplicates| duplicates.uniq_by! {|obj| obj.id}}.flatten.each {|duplicate| user.groups.delete(duplicate)}

Manipulating ActiveRecords using only a few attributes with Ruby on Rails

For caching matters, I'm caching an array of the attributes of the objects I need:
friends = [{:id => 4, :name => "Kevin"}, {:id => 12, :name => "Martin"}, …]
Is it possible to have a list of Users using this array, so that I can use Ruby methods? For instance, I usually get a list of non-friends with this:
non_friends = User.all - current_user.friends
Here, current_user.friends would be replaced by the cached array, only with the cached attributes:
friends = [
#<User id: 4, name: "Kevin", created_at: nil, updated_at: nil, email: nil>,
#<User id: 12, name: "Martin", created_at: nil, updated_at: nil, email: nil>,
…
]
Is it possible? Is it a good approach to caching? (a big list of ActiveRecords doesn't fit into a 1MB Memcache chunk.)
Thank you,
Kevin
edit: The idea behind this is to use a sorted/processed list of 2000 ActiveRecords around which my app heavily uses, but since it doesn't fit into a Memcache chunk, I'm trying to cache the interesting attributes only as an array. Now, how can I use this array like it was an ActiveRecord array?
Well, you can just cache the User IDs and then exclude these IDs in your finder conditions. In your example, assuming you have a friends array of hashes containing ids and names:
friend_ids = friends.map{ |f| f[:id] }
if friend_ids.empty?
non_friends = User.all
else
non_friends = User.all(:conditions => ['id NOT IN (?)', current_user.friend_ids])
end

Resources