In my Rails app, I only require users to enter email and name upon signup, but then give them the option to provide fuller contact details for their profile. Therefore, I have a User.rb model that has an association with Contact.rb, namely,
User.rb
 has_one :contact
Contact.rb
 belongs_to :user
Contact.rb has the predictable fields you might expect such as address, postal code etc, but it also stores the province_id for a relation with the Province.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 that way (rather than storing the name of the province as a "string" on contact.rb) so that I could more easily (so I thought) categorize users by province.
In the show action of one of the artists_controller, I do the following to check whether the user is trying to sort by province and then call an artists_by_province method that does a search
    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 on the User.rb model that it calls if a province id is passed in
scope :artists_by_province, lambda {|province|
  joins(:contact).
  where( contact: {province_id: province},
         users: {sculptor: true})
  }
However it gives me this error:
Could not find table 'contact'
If I make contacts plural
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 anyone tell me what I'm doing wrong when I'm making this query?
Update: I changed some of the details after posting because my copy and paste had some problems with it
P.S. ignore the fact that I'm searching for a 'sculptor.' I changed the names of the user types 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
The problem was fixed  by using contact (singular) in the join and contacts (plural) in the where clause. I'm guessing 'contact' (singular) reflects the has_one association between User.rb and Contact.rb, whereas 'contacts' is used in the where clause to represent the name of the table, which is always plural.
User.rb
  has_one :contact
  scope :artists_by_province, lambda {|province|
  joins(:contact).
  where( contacts: {province_id: province},
         users: {sculptor: true})
  }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With