ruby on rails - how to join an associated table with a has_one association -


in rails app, require users enter email , name upon signup, give them option provide fuller contact details profile. therefore, have user.rb model has association contact.rb, namely,

user.rb

 has_one :contact 

contact.rb

 belongs_to :user 

contact.rb has predictable fields might expect such address, postal code etc, stores province_id relation province.rb model,

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 way (rather storing name of province "string" on contact.rb) more (so thought) categorize users province.

in show action of 1 of artists_controller, following check whether user trying sort province , call artists_by_province method search

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

this method on user.rb model calls if province id passed in

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

however gives me error:

could not find table 'contact' 

if make contacts plural

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

this error

association named 'contacts' not found; perhaps misspelled it? 

can tell me i'm doing wrong when i'm making query?

update: changed of details after posting because copy , paste had problems it

p.s. ignore fact i'm searching 'sculptor.' changed names of user types question.

from schema.rb

create_table "contacts", :force => true |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 fixed using contact (singular) in join , contacts (plural) in clause. i'm guessing 'contact' (singular) reflects has_one association between user.rb , contact.rb, whereas 'contacts' used in clause represent name of table, plural.

user.rb

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

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -