database - Non-normalized association with legacy tables in Rails and ActiveRecord -


I am building rail applications to access a heritage system. Data models have customers, which may have one or more subscriptions. A membership is always related to one and only one customer. Although this is not necessary, this association has been displayed through an interactive table "Subscription", which does not have an ID column:

  column | Type | Modifier ----------------- + --------- + ----------- customer_id | Integer | No empty subscription_id | Integer | No blank  

I have this code and it is coded in both the customer and the subscription as the Handset_blog_t_a plus declarations

  class clients & lt; Activerecord :: Base has_and_belongs_to_many: Subscriptions ,: join_table = & gt; "Subscribe",: foreign_key => "Customer_id" ,: association_foreign_key = & gt; "Subscription_id" end class membership & lt; Activerecord :: Base has_and_belongs_to_many: Customer ,: join_table = & gt; "Subscribe",: foreign_key => "Subscription_id" ,: association_foreign_key = & gt; "Customer_ID" end  

I have the problem that there may be only one customer for each membership, not many, and the most important one in the table to join with the customer ID Line. And thus, I do not want "subscriber" on a subscription that gives an array of customer (actually the highest customer), I really want to relate to the "customer" who is related to the customer.

- Thomas

/ Div>

I do not know if you can do this with ActiveRecord , but if you For this problem, you can define the customer method membership model:

  class membership & lt; Activerecord :: Base has_and_belongs_to_many: Customer ,: join_table = & gt; "Subscribe",: foreign_key => "Subscription_id" ,: association_foreign_key = & gt; "Customer_id" def customer self Customer The first end date  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -