Question

In an example database, associations are as follows: an Owner has_many homes and Home belongs_to Owner. I know I can call owner.homes to get a list of all their homes but in my View I only seem to be able to call the home.owner_id to get anything from the owner object.

Is there a way to get around this?

Home model:

belongs_to :owner, 
 inverse_of: :homes

params look like

def home_params
    params.require(:home).permit(:street_address, :city, :state, :postal_code, :description, :owner_id)
  end
Was it helpful?

Solution

The ability to do home.owner is a natural outcome of what you've described, just as is owner.homes. This is all covered in http://guides.rubyonrails.org/association_basics.html

If you can do home.owner_id, but not home.owner, that means that your database is set up properly, but you're missing the belongs_to :owner call in your Home model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top