I've imported an Excel spreadsheet that has one-to-many relationship entries. For example, a business has one legal name but multiple locations with a DBA name for each location. There is a record for each DBA location. I'm filtering through the input data creating a single entry for each legal business name in one table and creating a business location entry for each DBA location. I'm trying to manually assigned the record ID for a legal business into each of its DBA business location records.
Here is my problem. When I try the following:
#dba_business.legal_business_id = #legal_business.id
I get the following error.
undefined method `id' for #<LegalBusiness::ActiveRecord_Relation:0x007fe1f2cc3770>
I tried the following but the #dba_business.legal_business_id ends up blank instead of putting the record ID value in the field.
#dba_business.legal_business_id = #legal_business
Legal_Business is set up with has_many :dba_business and DBA_business :belongs_to legal_business.
I used the debug.inspect command to see the attributes, logger.debug "LEGAL BUSINESS: #{#legal_business.inspect}", and you can clearly see the ID field defined as an attribute.
LEGAL BUSINESS: *#<ActiveRecord::Relation [#<LegalBusiness id: 58722, user_id: nil, legal_name:.........*
I'm using PostgreSQL 9.3, Rails 4.1, Ruby 2.1.1 with rvm. Any suggestions appreciated.
i recommend reading this: http://nofail.de/2013/10/debugging-rails-applications-in-development/
and then look at your debug message:
LEGAL BUSINESS: *#<ActiveRecord::Relation [#<LegalBusiness id: 58722, user_id: nil, legal_name:.........*
HINT: where are brackets [] usually used?
I solved the problem. The result of a quick copy-and-paste. I had an "#" in the wrong place.
#object = #Model.create()
and it should have been
#object = Model.create ()
Related
I have a general MongoDB question as I have recently found an issue with how I store things.
Currently, there is a collection called spaces like this:
{
_id: 5e1c4689429a8a0decf16f69,
challengers: [
5dfa24dce9cbc0180fb60226,
5dfa26f46719311869ac1756,
5dfa270c6719311869ac1757
],
tasks: [],
owner: 5dfa24dce9cbc0180fb60226,
name: 'testSpace',
description: 'testSpace'
}
As you can see, this has a challengers array, in which we store the ID of the User.
Would it be okey, if instead of storing the ID, I would store the entire User object, minus fields such as password etc?
Or should I continue with this reference path of referring to the ID of other documents?
The problem I have with this, is that when I want to go through all the spaces that a user has, I want to see what members are a part of that space (challengers array). However, I receive the IDS instead of name and email obviously. I am therefore struggling with sending the correct data to the frontend (I have tried doing some manual manipulation without luck).
So, if I have to continue the path of reference, then I will need to solve my problem somehow.
If it is okey to store the entire object in the array, It would be a lot easier.
HOWEVER, I want to do what is the best practice.
Thank you everyone!
I am using mongodb and odata.
I want to select name field alone for particular user id. (i.e) select name from userdata where userid=1;
/*my collection schema - userdata*/
{
id:number,
userid:string,
name:string,
data:object
}
I tried http://localhost:27017/userdata?$format=json&$filter=userid eq '1'&$select=name
Instead of getting name file alone I got whole object/document that matches userid=1. What I am doing wrong here?
I spoted the problem after the comment from #jps.
The issue is not with query, the problem is with data model which I am using is mismatching with my database schema (i.e) I missed out name field in model, so it is returning whole collection.
Now model is fixed, so the service is responding back with names for given userid.
I'm trying to use ActiveRecord's find_or_create_by method and I need the ID of returned record. When this method creates a new record this happens. However, if the record already exists, then the ID (along with the created_at and updated_at columns) is nil. All other data is returned just fine. Why is this? I can see it in my database and it has an ID. When I use find_by to get the record, the ID is returned. Is this a bug or am I missing something?
I know this is an old question, but I had the same issue and it was a pretty simple user error.
The id of your object will be nil if no persisted object was found for the selected attributes, but it can't be fully created (initialized and saved to the db) because it doesn't pass validations.
TLDR; If you use Model.find_or_create_by(attributes) and wind up with an object that has id: nil, check the errors on it, adjust your attributes, and happy coding.
As RachelS says but worse. This seems to happen if validations fail regardless if the record is found first. This is likely to come up if you have uniqueness validations on a column.
class Model < ApplicationRecord
validates :name, uniqueness: true
end
record = Model.create_or_find_by(name: 'test')
record.id # 1
record2 = Model.create_or_find_by(name: 'test')
record2.id # nil
record2.errors # #<ActiveModel::Errors [#<ActiveModel::Error attribute=name, type=taken, options={:value=>"test"}>]>
Unfortunately, this seems intended but basically makes create_or_find_by unusable as you'll normally want to have uniqueness validation on a unique indexed column which is a prerequisite to create_or_find_by in the first place...
According to the documentation, there is a Business Rule for Vendor and Employee that says
The name, first name, or last name field should not be blank.
Considering that NAME is required for create, the question is ¿Does this mean that ALL three properties have to have a value on Create?
We understood that, so we ran some tests. Creating a record with the three properties populated has no problem. The issue comes when we try so insert a null value for GivenName and FamilyName (first name and last name).
Quickbooks seems to take the NULL values as valid, and when inserting the record is not returning any error. The problem is that the record that was inserted never shows in the QBD UI, but is seems like it persists somewhere, because if we try to insert it again we get a "duplicate name" error.
Not sure if this is an issue or an expected behaviour... Any hints??
Thanks
QuickBooks for windows has some legacy behavior to be aware of. The First, Last and Name combines for a unique identifier accross Customers, Vendors and Employees. So you should fill in ALL the information you have. Any attempts to insert a record with the same F or L name, will fail if there is a Customer, Vendor or Employee with the same F & L.
Second, you mentioned that the record doesn't show in QuickBooks for Windows?
Did it sync successfully? Did you check the error state of the record you inserted?
Did you look at the sync logs to see if it failed to sync?
Lastly, you need to check all three objects for a user with the same F & L name if you are getting a duplicate error. Normally you would check to see if the customer/vendor/employee exists firsts so you can update the existing or determine if it is in fact a new record.
hope that helps
Jarred
Given that an ABRecordID can change between cloud syncs and under other circumstances out of my control, how can I maintain a long-term reference to an IOS address book record?
Apple provides the following guidance:
"The recommended way to keep a long-term reference to a particular record is to store the first and last name, or a hash of the first and last name, in addition to the identifier. When you look up a record by ID, compare the record’s name to your stored name. If they don’t match, use the stored name to find the record, and store the new ID for the record."
But I don't understand this guidance. If the address book can have duplicate names in it AND since users can modify the name in a record how could this advice work?
For example, if the user modifies the name of an address book record my routine will fail to find it by ABRecordID so if I think search by the name hash I stored couldn't I find a duplicate name instead of the new ABRecordID for that specific record I previously referenced?
In the end, what is the BEST way to get a long-term reference to an IOS AddressBook record? And if the above advice really does work what am I missing?
The most robust (yet not completely failsafe) approach would be to come up with a priority ranking of ABRecord fields and store as much from that list as is available, along with the ABRecordID, into your own (hashed) private record format. When retrieving a private record (or at another convenient time), you can verify that the private record matches the ABRecord and work through a series of fallback checks to ensure it's accurate.
Example priority ranking:
ABRecordID
FirstName
LastName
PhoneNumber
ZipCode
When retrieving a record you can first match the ABRecordID. If that returns no results, you can do a search for FirstName + LastName. You can then match those results against PhoneNumber... etc. In this way you could potentially distinguish between 2 Bob Smiths, as they may have different phone numbers (or one may not have a phone number). Of course, depending on how long your priority list is, the more robust this mechanism will be.
The last resort would be prompting the user to distinguish between 2 Bob Smiths with brand new ABRecordID's whose records are otherwise identical -- after all, such an inconvenient prompt would be far more friendly than allowing the User to contact the wrong Bob Smith (and as I said, would be a last resort).
This solution for AB may involve some synchronization issues, however.
This is a familiar problem for anyone who has worked with the iOS Media Player. Specifically MPMediaItems in the User's Music Library have a property MPMediaItemPropertyPersistentID which the docs describe as:
The value is not guaranteed to persist across a sync/unsync/sync cycle.
In other words, the PersistentID is not guaranteed to be persistent. Solutions for this include doing similar fallback checks on MediaItem properties.
The RecordID only get changed either on delete or reset, when this is done all the new record(s) will have new createdProperty and modifiedProperty as well.
While I am reading the address book for the first time, I will save all entries of the record along with RecordID in my database.
I will save the last time the contacts synced from contacts to my database(name it something: lastSyncedTime) and store it some where.
I am done with syncing the contacts for the first time, now do the following for syncing anytime in future.
while Iterating through all records,
check createdTime(kABPersonCreationDateProperty) vs lastSyncedTime. If createdTime > lastSyncedTime, store the recordID in a "newRecords" NSArray.
If !(step 1) then check modifiedDate(kABPersonModificationDateProperty) vs lastSyncedTime. If modifiedDate > lastSyncedTime, then store the recordID in a "modifiedRecords" NSArray.
if !(1) && !(2) store all recordID in a "unModifiedRecords".
Now I will read all the contacts from my local database,
I will delete all local database records that are not find either in "modifiedRecords" or in "unModifiedRecords".
I will update all "modifiedRecords" in the local database.
I will create new records for all records in "newRecords".
Update the lastSyncedTime accordingly.
The documentation is communicating to you that you can't count on ABRecordID as a permanent identifier.
Consider this scenario: The user has a record for "Bob Smith". The user then deletes his "Bob Smith" record and then imports his contacts from his computer (creating a new ID) through iTunes sync.
So if you want to keep a permanent reference to an existing contact, you can keep a reference to the name and id as a hint that it is the same record you used before- but there is no real permanent reference.
If you keep a permanent reference to an address book contact, you must always be ready to deal with the fact that it may not be the same contact you used before.
Refer :
https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/DirectInteraction.html#//apple_ref/doc/uid/TP40007744-CH6-SW2
Clearly tells you how to handle it.