Rails has_many belongs_to foreign_key primary_key confusion -
I was unable to setup my table using the ID field as the primary table (I mean that they are there But they do not mean anything for business) that means both sales and customer tables come from our Microsoft SQL database.
In the sales table, I have a field called 'customer_id' which corresponds to 'Customer_ID' (not 'id' field) in the Customer table. My 'customer_id' field is set to datatype integer, but in migration I have never specified whether they were key. I have only indexed them.
The sale comes under the customer and the customer has many sales so I am trying to create the association between has_many and belongs_to.
sale.rb
Classroom Sale & lt; ActiveRecord :: Base is_to: Customer Prathamik_ki: "Grahk_aidi" Videshi_ki: "Grahk_aidi" end customer.rb
Customer & lt; The ActiveRecord :: Base has_many: sales give the end
However, when I console the rail and I'll try to test it so I am not able to SQL for customer identification I searched the ID from the customer table only.
Rail Console
2.0.0-p353: 001 & gt; Select C = Customer.Find_Bee Gehr_ID (400123) Customer Load (2.0 MMS) "Customer". * "Customers" Customers "WHERE" "Customer_ID" = 400123 LIMIT 1 = & gt; # & Lt; Customer ID: 16271, CUSTOMER_ID: 400,123, customer_name: "ABC", customer_street: "ABC", customer_suburb: "ABC", customer_state: "ABC", customer_postcode: "ABC", dispatch_location_price_list: "ABC", customer_selling_price_list: "ABC" , customer_status: "ABC", line_of_business_id: "ABC", line_of_business: "ABC", market_segment_id: "ABC", market_segment_name: "other", customer_rep_id: 123, customer_rep_name: "ABC" 2.0.0-p353: 002 & gt; C.sales Sales Load (7.1ms) ** SELECT "Sale". * "Sales" from "Sale" Where "Sale" "Customer_id" = $ 1 [["customer_id", 16271]] ** = & gt; # & Lt; ActiveRecord :: Association :: Collection Proxy [] & gt;
In particular, I just [[ "customer_id ', 400123]] should last bit to say.
I know it's probably obvious, but as I am still learning that a lot of this stuff looks a little bit to sync (in many cases I have just answered my question by asking, but not tonight). Any suggestions would be greatly appreciated.
In my research I have found the API:
4.2.2.7: primary_key
conference The area, the ID column holding the primary key of this model assumes that rail. You can override this and specify clearly the primary key is the primary key.
I think I should use primary_key because there is no area to be seen using 'id' field.
I have also read but I think it may be slightly different from my situation Have .
Do you have
in the
customer model
Attempted to add self.primary_key =: customer_id ?
Comments
Post a Comment