0

I am trying to do a join table. My first table is called web_forms with column account_id. Second table is called required_fields with column web_form_id.

I am currently getting the error...

RequiredFields.find(226) ActiveRecord::RecordNotFound: Couldn't find RequiredField with id=226

class WebForm < ActiveRecord::Base
  has_many      :required_fields,
                class_name: RequiredField,
                foreign_key: :web_form_id
end


class RequiredField < ActiveRecord::Base
   has_and_belongs_to_many :web_forms,
                           class_name: WebForm,
                           foreign_key: :web_form_id,
                           association_foreign_key: :account_id
end

I read through this and it confused me more.

http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference

Any help would be great thanks.

stooky
  • 1
  • 3
  • 1
    are you sure you have a record with `id` equal to `226`, rails is telling you that it does not exist. – davidhu Sep 22 '16 at 23:40
  • in web_forms the account_id is 226 but in required_fields its web_form_id 226. Am I going about this the wrong way? – stooky Sep 22 '16 at 23:45
  • so you are trying to look up required_fields by foreign key? looks like you query required_field using its id, which is automatically assigned each time you create a record. – davidhu Sep 22 '16 at 23:52
  • Ah I see how would I go about searching it by web_form_id instead of just id? – stooky Sep 22 '16 at 23:57
  • can you show me the code you are using right now to search – davidhu Sep 22 '16 at 23:58
  • you can try using the association eg: `form = WebForm.find(226); form.required_fields` will give you all the required fields for any given web-form (split on the `;`, that's just there because code formatting in comments is awful). – Taryn East Sep 23 '16 at 00:00
  • 1
    Alternatively, to just use it from RequiredField you'd do: `RequiredField.where(:web_form_id => 226)` – Taryn East Sep 23 '16 at 00:01
  • form = WebForm.find(226);form.required_fields tried this I am getting undefined method `required_fields' for # – stooky Sep 23 '16 at 04:17

0 Answers0