-1

I have 2 tables scenarios and rental_details.

The relationships are:

  • rental_details belongs_to scenarios
  • scenarios has_one rental_detail

In the rental_details table I have the scenario_id as a column (note this is as data type INTEGER and not FOREIGN_KEY).

If I run the following in rails console:

rental_details_1 = RentalDetail.find(123)
rental_details_1.id
=> 123
rental_details.scenario.id
=> 22 #i.e. this is linked to scenario with id = 22

However, if I do the following:

scenario_1 = Scenario.find(22)
scenario_1.id
=> 22
scenario_1.rental_details.id

I receive the following error:

NoMethodError: undefined method `rental_details' for

Did you mean? rental_detail rental_detail=

If I change it to scenario_1.rental_details.id I still receive an error.

If I have a one-to-one relationship such as this, can I not access the rental_details table from the scenario object? I thought that the foreign key is in the "belongs_to" table I can automatically call its contents from the "has_one" table's object.

(Sorry, I'm new to coding so my terminology is most probably wrong at points).

Patrick
  • 5,526
  • 14
  • 64
  • 101
DAR_ad
  • 59
  • 1
  • 6

2 Answers2

0

This is expected behavior.

You're still accessing the correct object — a RentalDetail object. If a Scenario has_many RentalDetail, then it'd be accessed using the pluralized form.

Josh Brody
  • 5,153
  • 1
  • 14
  • 25
  • Thanks for the response. If this is the expected behaviour, how can I get the rental_details id for the scenario object? – DAR_ad Jan 21 '18 at 19:04
0

This is the convention:

Scenario has_one RentalDetail => scenario_1.rental_detail.id (has_one docs). It uses the singular form of RentalDetail.

class Scenario < ActiveRecord::Base
 has_one :rental_detail
end

Scenario has_many RentalDetail => scenario_1.rental_details.pluck(:id) (has_many docs). It uses the pluralized version of RentalDetail.

class Scenario < ActiveRecord::Base
 has_many :rental_details
end

As I said, this is the convetion but nobody stops you from naming the associations as you like. You just need to tell the ActiveRecord to use correct class_name, primary_key, foreign_key, etc. Check the docs.

razvans
  • 3,172
  • 7
  • 22
  • 30
  • I have checked the docs but the issue here is that I have used the correct convention (i.e. everything is in the singular for this one-to-one relationship), however, I still cannot access the rental_detail data from the scenario object I create. – DAR_ad Jan 23 '18 at 05:49
  • You have to name you association at "singular", rental_detail. I has to work. That is the convention. `scenario_1.rental_detail.id` - there's no `s` at the end of rental_detail. – razvans Jan 23 '18 at 06:57
  • Here are the models and associations I am currently using: class Scenario < ApplicationRecord belongs_to :property has_one :rental_detail end and class RentalDetail < ApplicationRecord belongs_to :scenario end As you can see, everything is in the singular. I still receive the NoMethodError – DAR_ad Jan 23 '18 at 18:23
  • I've rerun everything and it now works. I can confirm that once all is in the singular it works. – DAR_ad Jan 23 '18 at 18:49