How to join a linked table with has_one association

In my Rails application, I require users to enter an email address and name when registering, but then give them the opportunity to provide more complete contact information for their profile. Therefore, I have a User.rb model that has a connection with Contact.rb, namely

User.rb

has_one :contact 

Contact.rb

  belongs_to :user 

Contact.rb has predictable fields that you can expect, such as address, zip code, etc., but it also saves region_id for communication with the Region.rb model, so

Contact.rb

  attr_accessible :address, :city, :mobile, :postalcode, :province_id, :user_id belongs_to :user belongs_to :province 

Province.rb

 has_many :contacts 

I did it this way (instead of storing the province name as a "string" on contact.rb) so that I could more easily (as I thought) categorize users by province.

In the show action of one of artist_controller, I do the following to check if the user is trying to sort by province, and then calls the artist_by_province method, which searches

  if params[:province_id] province = params[:province_id] province = province.to_i #convert string to integer @artistsbyprovince = User.artists_by_province(province) else @artists = User.where(:sculptor => true) end 

This is the method in the User.rb model that it calls if the province id is passed in

 scope :artists_by_province, lambda {|province| joins(:contact). where( contact: {province_id: province}, users: {sculptor: true}) } 

However, this gives me this error:

 Could not find table 'contact' 

If I make contacts multiple

 scope :artists_by_province, lambda {|province| joins(:contacts). where( contacts: {province_id: province}, users: {sculptor: true}) } 

This error

 Association named 'contacts' was not found; perhaps you misspelled it? 

Can someone tell me what I am doing wrong when I make this request?

Update: I changed some details after posting because I had copy and paste problems

PS ignore the fact that I'm looking for a "sculptor". I changed the user type names for the question.

from schema.rb

 create_table "contacts", :force => true do |t| t.string "firm" t.string "address" t.string "city" t.string "postalcode" t.string "mobile" t.string "office" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "province_id" end 
+7
source share
2 answers

The problem was fixed with contact (singular) in the connection and contacts (plural) in the where clause. I assume that the โ€œcontactโ€ (singular) reflects the has_one relationship between User.rb and Contact.rb, while the โ€œcontactsโ€ are used in the where clause to represent a table name that is always plural.

User.rb

  has_one :contact scope :artists_by_province, lambda {|province| joins(:contact). where( contacts: {province_id: province}, users: {sculptor: true}) } 
+7
source

Can you try the following?

 scope :artists_by_province, lambda {|province| joins(contact: {province: { id: province}}). where(sculptor: true) } 
0
source

All Articles