type missmatch - class expected, but got string

17 views
Skip to first unread message

tom

unread,
Mar 28, 2017, 1:38:09 PM3/28/17
to Ruby on Rails: Talk
hi,i am trying to use ror on a legacy system.


model:
   belongs_to :ProvincialTaxAreaCode, class_name: "TaxArea", foreign_key: "ProvincialTaxAreaCode" #, primary_key: "ProvincialTaxAreaCode"

ProvincialTaxAreaCode is a varchar(20)


html-slect:
<select class="select optional form-control" name="record[ProvincialTaxAreaCode]" id="record_ProvincialTaxAreaCode"><option value=""></option>
<option value="1">test- 123</option></select>

error:
ActiveRecord::AssociationTypeMismatch (TaxArea(#109333370) expected, got String(#70918210)):

how can i still use the varchar field without converting it???



Hassan Schroeder

unread,
Mar 28, 2017, 2:56:11 PM3/28/17
to rubyonrails-talk
On Tue, Mar 28, 2017 at 10:37 AM, tom <toma...@gmail.com> wrote:

> model:
> belongs_to :ProvincialTaxAreaCode, class_name: "TaxArea", foreign_key:
> "ProvincialTaxAreaCode" #, primary_key: "ProvincialTaxAreaCode"
>
> ProvincialTaxAreaCode is a varchar(20)

?? A model "belongs_to" another model, not a string.

This might help:

http://guides.rubyonrails.org/association_basics.html#the-belongs-to-association

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

tom

unread,
Mar 28, 2017, 3:06:54 PM3/28/17
to Ruby on Rails: Talk
where do you see a string?
association is called :ProvincialTaxAreaCode (because of simple_form_for
the associated class is called TaxArea
the fkey is ProvincialTaxAreaCode (varchar20)



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yDsk1f_uGp3Ypzt1K3sMg873XA7OYV4uMVjaojTm74q2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Hassan Schroeder

unread,
Mar 28, 2017, 4:13:28 PM3/28/17
to rubyonrails-talk
On Tue, Mar 28, 2017 at 12:05 PM, tom <toma...@gmail.com> wrote:
> where do you see a string?

To repeat quoting you:

> model:
> belongs_to :ProvincialTaxAreaCode

> ProvincialTaxAreaCode is a varchar(20)

Did you look at the Rails guide link I included?

nanaya

unread,
Mar 28, 2017, 5:19:51 PM3/28/17
to rubyonra...@googlegroups.com
Hi,
I don't think you can use same name for attribute and association
because it'll be confusing (what does model.ProvincialTaxAreaCode mean?
Is it the attribute or the association?). Try naming the association
something else.

Mike Stefanskiy

unread,
Mar 29, 2017, 5:20:42 AM3/29/17
to Ruby on Rails: Talk
Hi. You should use different names for association and for foreign key for this association. Looks like they are overlapping, just rename association name to fit ar conventions - and it should work.
Reply all
Reply to author
Forward
0 new messages