Discussion:
update_or_create by ID
Aryk Grosz
2017-03-01 23:50:07 UTC
Permalink
Whenever I use this plugin, if I'm trying to check based on the primary
key, I get an error because it always tries to set the foreign key
attribute on the resulting record (whether it's being created or updated).

Was this intentional behavior? If you have incoming attrs and want to check
for the primary key and pull the record based on that, does it have to be
done manually? Or is there something in the library that I'm missing?
--
You received this message because you are subscribed to the Google Groups "sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+***@googlegroups.com.
To post to this group, send email to sequel-***@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.
Aryk Grosz
2017-03-02 00:06:17 UTC
Permalink
I guess what I'm going for is something like this:


def self.update_or_create_with_id(attrs)
id = attrs.delete(primary_key)
record = block_given? ? yield(id) : self[id]
record.update(attrs) if record # can return nil
record || create(attrs)
end
Post by Aryk Grosz
Whenever I use this plugin, if I'm trying to check based on the primary
key, I get an error because it always tries to set the foreign key
attribute on the resulting record (whether it's being created or updated).
Was this intentional behavior? If you have incoming attrs and want to
check for the primary key and pull the record based on that, does it have
to be done manually? Or is there something in the library that I'm missing?
--
You received this message because you are subscribed to the Google Groups "sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+***@googlegroups.com.
To post to this group, send email to sequel-***@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.
Jeremy Evans
2017-03-02 00:35:27 UTC
Permalink
Post by Aryk Grosz
Whenever I use this plugin, if I'm trying to check based on the primary
key, I get an error because it always tries to set the foreign key
attribute on the resulting record (whether it's being created or updated).
Trying to check based on the primary key with update_or_create doesn't make
sense unless you are unrestricting access to the primary key. If you do:

update_or_create(:id=>1, :name=>'Foo')

It's going to look for a record with (id = 1 AND name = 'Foo'), and if it
does not find one, it's going to insert a record with (id = 1, name = 'Foo')

It sounds like you want to try to find a record with a given primary key,
update the record matching the primary key with the given attributes if it
doesn't exist, and create a new record with a different primary key if it
doesn't exist. That's not what update_or_create is designed to do.
Post by Aryk Grosz
Was this intentional behavior? If you have incoming attrs and want to
check for the primary key and pull the record based on that, does it have
to be done manually? Or is there something in the library that I'm missing?
The current behavior is intentional. For your needs, I would recommend
using the custom method you posted instead.

Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups "sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+***@googlegroups.com.
To post to this group, send email to sequel-***@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.
Aryk Grosz
2017-03-02 14:00:32 UTC
Permalink
Makes sense, will do that. Just wanted to check if maybe I was missing
something...
Post by Jeremy Evans
Post by Aryk Grosz
Whenever I use this plugin, if I'm trying to check based on the primary
key, I get an error because it always tries to set the foreign key
attribute on the resulting record (whether it's being created or updated).
Trying to check based on the primary key with update_or_create doesn't
make sense unless you are unrestricting access to the primary key. If you
update_or_create(:id=>1, :name=>'Foo')
It's going to look for a record with (id = 1 AND name = 'Foo'), and if it
does not find one, it's going to insert a record with (id = 1, name = 'Foo')
It sounds like you want to try to find a record with a given primary key,
update the record matching the primary key with the given attributes if it
doesn't exist, and create a new record with a different primary key if it
doesn't exist. That's not what update_or_create is designed to do.
Post by Aryk Grosz
Was this intentional behavior? If you have incoming attrs and want to
check for the primary key and pull the record based on that, does it have
to be done manually? Or is there something in the library that I'm missing?
The current behavior is intentional. For your needs, I would recommend
using the custom method you posted instead.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups "sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+***@googlegroups.com.
To post to this group, send email to sequel-***@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.
Loading...