Re: [Rails] Re: reset association cache for polymorphism

2015-09-21 Thread Oto Iashvili
it doesnt work either, still same problem

On Tuesday, September 22, 2015 at 12:40:39 AM UTC+2, Bráulio Bhavamitra 
wrote:
>
> replace :table with the right table name?
>
> On Mon, Sep 21, 2015 at 2:27 PM Oto Iashvili  > wrote:
>
>> so I tried this
>>
>> Class Pencil
>> self.table_name = :table
>>   include DetailsInfo
>> type
>> color
>> end
>>
>> Class Notebook
>> self.table_name = :table
>>
>>   include DetailsInfo
>> size
>> ...
>> end
>>
>> but still same problem. Do I get it wrong ?
>>
>>
>> On Monday, September 21, 2015 at 5:20:22 PM UTC+2, Bráulio Bhavamitra 
>> wrote:
>>
>>> Do it inside the class
>>>
>>> On Mon, Sep 21, 2015 at 10:26 AM Oto Iashvili  
>>> wrote:
>>>
>> thanks for answer. But Im not sure tu understand
 where or when shall I call this ?
 cause im already doing

 DetailsInfo.table_name = 'pencil'


 On Monday, September 21, 2015 at 1:08:20 PM UTC+2, Bráulio Bhavamitra 
 wrote:

> Call self.table_name = :table on each model
>
> Em 22h52 dom, 20/09/2015, Oto Iashvili  
> escreveu:
>
 Thanks a lot for answer, Unfortunately, it is an  site existing for 
>> several years, so I cant change model, so I really need to be able to 
>> change "manually" the table_name or to make like a patch forcing to 
>> check 
>> if table_name has been changed and then update association cache 
>>
>>
>> On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:
>>>
>>>
>>>
>>> On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:

 Hi,

 I have a product class with category and  a polymorphic attributes 
 details_info, that depending on category go to one or another table to 
 get 
 the detail


 Class Product
 category
 details_info

 end

 Class DetailsInfo
 ...
 end

 Class Pencil

   include DetailsInfo
 type
 color
 end

 Class Notebook

   include DetailsInfo
 size
 ...
 end

 and to display products i do
 prds = Products.all
 prds.each do |pr|

 if pr.category == 'pencil'
 DetailsInfo.table_name = 'pencil'

 else if pr.category == 'notebook'

 DetailsInfo.table_name = 'notebook'
 end

 end

 (this is just kind of algo I use)

 All this was working fine but with rails > 4.2 , it doesnt work 
 anymore.
 The first time pr.details_info will be called, it seems that the 
 table name is cached somewhere, and then all other products are using 
 the 
 same table_name. When I check during loop, I can see that 
 DetailsInfo.table_name is always correct, but still when 
 pr.details_info 
 will be called , it used the first table_name that was used. I can 
 also see 
 that the method table_name is called every step of the loop, and it 
 return 
 the good table_name, but still, the sql request search on bad table.

 How can I reset that ? Ive tried a lot a thing like 
 emptying association_cache, also different things 
 with reflect_on_all_associations, reset_column_information, reload, ...


>>> As mentioned on rails-core, I'd recommend looking into polymorphic 
>>> associations for this. Here's a possible example:
>>> ```
>>>   class Product < ActiveRecord::Base
>>> # the products table has two columns relevant here:
>>> #   * details_info_id, integer
>>> #   * details_info_type, string
>>> belongs_to :details_info, polymorphic: true
>>>   end
>>>
>>>   class Pencil < ActiveRecord::Base
>>> has_many :products, as: :details_info
>>>   end
>>>
>>>   class Notebook < ActiveRecord::Base
>>> has_many :products, as: :details_info
>>>   end
>>> ```
>>>
>>> Given those models, if you retrieve a list of Product objects you 
>>> should be able to access each one's `details_info` from the correct 
>>> tables.
>>>
>>> If you're still unsure how to proceed, more detail on the specifics 
>>> of your schema and desired functionality would be useful.
>>>
>>> --Matt Jones
>>>
>> -- 
>> 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-ta...@googlegroups.com.
>> To post to this group, send email to rubyonra...@googlegroups.com.
>
>
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rubyonrails-talk/a7638d

Re: [Rails] Re: reset association cache for polymorphism

2015-09-21 Thread Bráulio Bhavamitra
replace :table with the right table name?

On Mon, Sep 21, 2015 at 2:27 PM Oto Iashvili 
wrote:

> so I tried this
>
> Class Pencil
> self.table_name = :table
>   include DetailsInfo
> type
> color
> end
>
> Class Notebook
> self.table_name = :table
>
>   include DetailsInfo
> size
> ...
> end
>
> but still same problem. Do I get it wrong ?
>
>
> On Monday, September 21, 2015 at 5:20:22 PM UTC+2, Bráulio Bhavamitra
> wrote:
>
>> Do it inside the class
>>
>> On Mon, Sep 21, 2015 at 10:26 AM Oto Iashvili 
>> wrote:
>>
> thanks for answer. But Im not sure tu understand
>>> where or when shall I call this ?
>>> cause im already doing
>>>
>>> DetailsInfo.table_name = 'pencil'
>>>
>>>
>>> On Monday, September 21, 2015 at 1:08:20 PM UTC+2, Bráulio Bhavamitra
>>> wrote:
>>>
 Call self.table_name = :table on each model

 Em 22h52 dom, 20/09/2015, Oto Iashvili 
 escreveu:

>>> Thanks a lot for answer, Unfortunately, it is an  site existing for
> several years, so I cant change model, so I really need to be able to
> change "manually" the table_name or to make like a patch forcing to check
> if table_name has been changed and then update association cache
>
>
> On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:
>>
>>
>>
>> On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:
>>>
>>> Hi,
>>>
>>> I have a product class with category and  a polymorphic attributes
>>> details_info, that depending on category go to one or another table to 
>>> get
>>> the detail
>>>
>>>
>>> Class Product
>>> category
>>> details_info
>>>
>>> end
>>>
>>> Class DetailsInfo
>>> ...
>>> end
>>>
>>> Class Pencil
>>>
>>>   include DetailsInfo
>>> type
>>> color
>>> end
>>>
>>> Class Notebook
>>>
>>>   include DetailsInfo
>>> size
>>> ...
>>> end
>>>
>>> and to display products i do
>>> prds = Products.all
>>> prds.each do |pr|
>>>
>>> if pr.category == 'pencil'
>>> DetailsInfo.table_name = 'pencil'
>>>
>>> else if pr.category == 'notebook'
>>>
>>> DetailsInfo.table_name = 'notebook'
>>> end
>>>
>>> end
>>>
>>> (this is just kind of algo I use)
>>>
>>> All this was working fine but with rails > 4.2 , it doesnt work
>>> anymore.
>>> The first time pr.details_info will be called, it seems that the
>>> table name is cached somewhere, and then all other products are using 
>>> the
>>> same table_name. When I check during loop, I can see that
>>> DetailsInfo.table_name is always correct, but still when pr.details_info
>>> will be called , it used the first table_name that was used. I can also 
>>> see
>>> that the method table_name is called every step of the loop, and it 
>>> return
>>> the good table_name, but still, the sql request search on bad table.
>>>
>>> How can I reset that ? Ive tried a lot a thing like
>>> emptying association_cache, also different things
>>> with reflect_on_all_associations, reset_column_information, reload, ...
>>>
>>>
>> As mentioned on rails-core, I'd recommend looking into polymorphic
>> associations for this. Here's a possible example:
>> ```
>>   class Product < ActiveRecord::Base
>> # the products table has two columns relevant here:
>> #   * details_info_id, integer
>> #   * details_info_type, string
>> belongs_to :details_info, polymorphic: true
>>   end
>>
>>   class Pencil < ActiveRecord::Base
>> has_many :products, as: :details_info
>>   end
>>
>>   class Notebook < ActiveRecord::Base
>> has_many :products, as: :details_info
>>   end
>> ```
>>
>> Given those models, if you retrieve a list of Product objects you
>> should be able to access each one's `details_info` from the correct 
>> tables.
>>
>> If you're still unsure how to proceed, more detail on the specifics
>> of your schema and desired functionality would be useful.
>>
>> --Matt Jones
>>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.


> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/a7638d1e-2321-463f-a8dd-2e6b6e4e3421%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>
 --
>>> You received this message because you are sub

Re: [Rails] Re: reset association cache for polymorphism

2015-09-21 Thread Oto Iashvili
so I tried this

Class Pencil
self.table_name = :table
  include DetailsInfo
type
color
end

Class Notebook
self.table_name = :table
  include DetailsInfo
size
...
end

but still same problem. Do I get it wrong ?


On Monday, September 21, 2015 at 5:20:22 PM UTC+2, Bráulio Bhavamitra wrote:
>
> Do it inside the class
>
> On Mon, Sep 21, 2015 at 10:26 AM Oto Iashvili  > wrote:
>
>> thanks for answer. But Im not sure tu understand
>> where or when shall I call this ?
>> cause im already doing
>>
>> DetailsInfo.table_name = 'pencil'
>>
>>
>> On Monday, September 21, 2015 at 1:08:20 PM UTC+2, Bráulio Bhavamitra 
>> wrote:
>>
>>> Call self.table_name = :table on each model
>>>
>>> Em 22h52 dom, 20/09/2015, Oto Iashvili  
>>> escreveu:
>>>
>> Thanks a lot for answer, Unfortunately, it is an  site existing for 
 several years, so I cant change model, so I really need to be able to 
 change "manually" the table_name or to make like a patch forcing to check 
 if table_name has been changed and then update association cache 


 On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:
>
>
>
> On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:
>>
>> Hi,
>>
>> I have a product class with category and  a polymorphic attributes 
>> details_info, that depending on category go to one or another table to 
>> get 
>> the detail
>>
>>
>> Class Product
>> category
>> details_info
>>
>> end
>>
>> Class DetailsInfo
>> ...
>> end
>>
>> Class Pencil
>>
>>   include DetailsInfo
>> type
>> color
>> end
>>
>> Class Notebook
>>
>>   include DetailsInfo
>> size
>> ...
>> end
>>
>> and to display products i do
>> prds = Products.all
>> prds.each do |pr|
>>
>> if pr.category == 'pencil'
>> DetailsInfo.table_name = 'pencil'
>>
>> else if pr.category == 'notebook'
>>
>> DetailsInfo.table_name = 'notebook'
>> end
>>
>> end
>>
>> (this is just kind of algo I use)
>>
>> All this was working fine but with rails > 4.2 , it doesnt work 
>> anymore.
>> The first time pr.details_info will be called, it seems that the 
>> table name is cached somewhere, and then all other products are using 
>> the 
>> same table_name. When I check during loop, I can see that 
>> DetailsInfo.table_name is always correct, but still when pr.details_info 
>> will be called , it used the first table_name that was used. I can also 
>> see 
>> that the method table_name is called every step of the loop, and it 
>> return 
>> the good table_name, but still, the sql request search on bad table.
>>
>> How can I reset that ? Ive tried a lot a thing like 
>> emptying association_cache, also different things 
>> with reflect_on_all_associations, reset_column_information, reload, ...
>>
>>
> As mentioned on rails-core, I'd recommend looking into polymorphic 
> associations for this. Here's a possible example:
> ```
>   class Product < ActiveRecord::Base
> # the products table has two columns relevant here:
> #   * details_info_id, integer
> #   * details_info_type, string
> belongs_to :details_info, polymorphic: true
>   end
>
>   class Pencil < ActiveRecord::Base
> has_many :products, as: :details_info
>   end
>
>   class Notebook < ActiveRecord::Base
> has_many :products, as: :details_info
>   end
> ```
>
> Given those models, if you retrieve a list of Product objects you 
> should be able to access each one's `details_info` from the correct 
> tables.
>
> If you're still unsure how to proceed, more detail on the specifics of 
> your schema and desired functionality would be useful.
>
> --Matt Jones
>
 -- 
 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-ta...@googlegroups.com.
 To post to this group, send email to rubyonra...@googlegroups.com.
>>>
>>>
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/rubyonrails-talk/a7638d1e-2321-463f-a8dd-2e6b6e4e3421%40googlegroups.com
  
 
 .
 For more options, visit https://groups.google.com/d/optout.

>>> -- 
>> 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-ta...@googlegroups.com .
>> To post to this group, send email to rubyonra...@goog

Re: [Rails] Re: reset association cache for polymorphism

2015-09-21 Thread Bráulio Bhavamitra
Do it inside the class

On Mon, Sep 21, 2015 at 10:26 AM Oto Iashvili 
wrote:

> thanks for answer. But Im not sure tu understand
> where or when shall I call this ?
> cause im already doing
>
> DetailsInfo.table_name = 'pencil'
>
>
> On Monday, September 21, 2015 at 1:08:20 PM UTC+2, Bráulio Bhavamitra
> wrote:
>
>> Call self.table_name = :table on each model
>>
>> Em 22h52 dom, 20/09/2015, Oto Iashvili  escreveu:
>>
> Thanks a lot for answer, Unfortunately, it is an  site existing for
>>> several years, so I cant change model, so I really need to be able to
>>> change "manually" the table_name or to make like a patch forcing to check
>>> if table_name has been changed and then update association cache
>>>
>>>
>>> On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:



 On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:
>
> Hi,
>
> I have a product class with category and  a polymorphic attributes
> details_info, that depending on category go to one or another table to get
> the detail
>
>
> Class Product
> category
> details_info
>
> end
>
> Class DetailsInfo
> ...
> end
>
> Class Pencil
>
>   include DetailsInfo
> type
> color
> end
>
> Class Notebook
>
>   include DetailsInfo
> size
> ...
> end
>
> and to display products i do
> prds = Products.all
> prds.each do |pr|
>
> if pr.category == 'pencil'
> DetailsInfo.table_name = 'pencil'
>
> else if pr.category == 'notebook'
>
> DetailsInfo.table_name = 'notebook'
> end
>
> end
>
> (this is just kind of algo I use)
>
> All this was working fine but with rails > 4.2 , it doesnt work
> anymore.
> The first time pr.details_info will be called, it seems that the table
> name is cached somewhere, and then all other products are using the same
> table_name. When I check during loop, I can see that 
> DetailsInfo.table_name
> is always correct, but still when pr.details_info will be called , it used
> the first table_name that was used. I can also see that the method
> table_name is called every step of the loop, and it return the good
> table_name, but still, the sql request search on bad table.
>
> How can I reset that ? Ive tried a lot a thing like
> emptying association_cache, also different things
> with reflect_on_all_associations, reset_column_information, reload, ...
>
>
 As mentioned on rails-core, I'd recommend looking into polymorphic
 associations for this. Here's a possible example:
 ```
   class Product < ActiveRecord::Base
 # the products table has two columns relevant here:
 #   * details_info_id, integer
 #   * details_info_type, string
 belongs_to :details_info, polymorphic: true
   end

   class Pencil < ActiveRecord::Base
 has_many :products, as: :details_info
   end

   class Notebook < ActiveRecord::Base
 has_many :products, as: :details_info
   end
 ```

 Given those models, if you retrieve a list of Product objects you
 should be able to access each one's `details_info` from the correct tables.

 If you're still unsure how to proceed, more detail on the specifics of
 your schema and desired functionality would be useful.

 --Matt Jones

>>> --
>>> 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-ta...@googlegroups.com.
>>> To post to this group, send email to rubyonra...@googlegroups.com.
>>
>>
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/rubyonrails-talk/a7638d1e-2321-463f-a8dd-2e6b6e4e3421%40googlegroups.com
>>> 
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> 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+unsubscr...@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/afe95901-4752-4385-968d-3aaaede54a35%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To u

Re: [Rails] Re: reset association cache for polymorphism

2015-09-21 Thread Oto Iashvili
thanks for answer. But Im not sure tu understand
where or when shall I call this ?
cause im already doing

DetailsInfo.table_name = 'pencil'


On Monday, September 21, 2015 at 1:08:20 PM UTC+2, Bráulio Bhavamitra wrote:
>
> Call self.table_name = :table on each model
>
> Em 22h52 dom, 20/09/2015, Oto Iashvili  > escreveu:
>
>> Thanks a lot for answer, Unfortunately, it is an  site existing for 
>> several years, so I cant change model, so I really need to be able to 
>> change "manually" the table_name or to make like a patch forcing to check 
>> if table_name has been changed and then update association cache 
>>
>>
>> On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:
>>>
>>>
>>>
>>> On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:

 Hi,

 I have a product class with category and  a polymorphic attributes 
 details_info, that depending on category go to one or another table to get 
 the detail


 Class Product
 category
 details_info

 end

 Class DetailsInfo
 ...
 end

 Class Pencil

   include DetailsInfo
 type
 color
 end

 Class Notebook

   include DetailsInfo
 size
 ...
 end

 and to display products i do
 prds = Products.all
 prds.each do |pr|

 if pr.category == 'pencil'
 DetailsInfo.table_name = 'pencil'

 else if pr.category == 'notebook'

 DetailsInfo.table_name = 'notebook'
 end

 end

 (this is just kind of algo I use)

 All this was working fine but with rails > 4.2 , it doesnt work anymore.
 The first time pr.details_info will be called, it seems that the table 
 name is cached somewhere, and then all other products are using the same 
 table_name. When I check during loop, I can see that 
 DetailsInfo.table_name 
 is always correct, but still when pr.details_info will be called , it used 
 the first table_name that was used. I can also see that the method 
 table_name is called every step of the loop, and it return the good 
 table_name, but still, the sql request search on bad table.

 How can I reset that ? Ive tried a lot a thing like 
 emptying association_cache, also different things 
 with reflect_on_all_associations, reset_column_information, reload, ...


>>> As mentioned on rails-core, I'd recommend looking into polymorphic 
>>> associations for this. Here's a possible example:
>>> ```
>>>   class Product < ActiveRecord::Base
>>> # the products table has two columns relevant here:
>>> #   * details_info_id, integer
>>> #   * details_info_type, string
>>> belongs_to :details_info, polymorphic: true
>>>   end
>>>
>>>   class Pencil < ActiveRecord::Base
>>> has_many :products, as: :details_info
>>>   end
>>>
>>>   class Notebook < ActiveRecord::Base
>>> has_many :products, as: :details_info
>>>   end
>>> ```
>>>
>>> Given those models, if you retrieve a list of Product objects you should 
>>> be able to access each one's `details_info` from the correct tables.
>>>
>>> If you're still unsure how to proceed, more detail on the specifics of 
>>> your schema and desired functionality would be useful.
>>>
>>> --Matt Jones
>>>
>> -- 
>> 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-ta...@googlegroups.com .
>> To post to this group, send email to rubyonra...@googlegroups.com 
>> .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rubyonrails-talk/a7638d1e-2321-463f-a8dd-2e6b6e4e3421%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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+unsubscr...@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/afe95901-4752-4385-968d-3aaaede54a35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Re: reset association cache for polymorphism

2015-09-21 Thread Bráulio Bhavamitra
Call self.table_name = :table on each model

Em 22h52 dom, 20/09/2015, Oto Iashvili 
escreveu:

> Thanks a lot for answer, Unfortunately, it is an  site existing for
> several years, so I cant change model, so I really need to be able to
> change "manually" the table_name or to make like a patch forcing to check
> if table_name has been changed and then update association cache
>
>
> On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:
>>
>>
>>
>> On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:
>>>
>>> Hi,
>>>
>>> I have a product class with category and  a polymorphic attributes
>>> details_info, that depending on category go to one or another table to get
>>> the detail
>>>
>>>
>>> Class Product
>>> category
>>> details_info
>>>
>>> end
>>>
>>> Class DetailsInfo
>>> ...
>>> end
>>>
>>> Class Pencil
>>>
>>>   include DetailsInfo
>>> type
>>> color
>>> end
>>>
>>> Class Notebook
>>>
>>>   include DetailsInfo
>>> size
>>> ...
>>> end
>>>
>>> and to display products i do
>>> prds = Products.all
>>> prds.each do |pr|
>>>
>>> if pr.category == 'pencil'
>>> DetailsInfo.table_name = 'pencil'
>>>
>>> else if pr.category == 'notebook'
>>>
>>> DetailsInfo.table_name = 'notebook'
>>> end
>>>
>>> end
>>>
>>> (this is just kind of algo I use)
>>>
>>> All this was working fine but with rails > 4.2 , it doesnt work anymore.
>>> The first time pr.details_info will be called, it seems that the table
>>> name is cached somewhere, and then all other products are using the same
>>> table_name. When I check during loop, I can see that DetailsInfo.table_name
>>> is always correct, but still when pr.details_info will be called , it used
>>> the first table_name that was used. I can also see that the method
>>> table_name is called every step of the loop, and it return the good
>>> table_name, but still, the sql request search on bad table.
>>>
>>> How can I reset that ? Ive tried a lot a thing like
>>> emptying association_cache, also different things
>>> with reflect_on_all_associations, reset_column_information, reload, ...
>>>
>>>
>> As mentioned on rails-core, I'd recommend looking into polymorphic
>> associations for this. Here's a possible example:
>> ```
>>   class Product < ActiveRecord::Base
>> # the products table has two columns relevant here:
>> #   * details_info_id, integer
>> #   * details_info_type, string
>> belongs_to :details_info, polymorphic: true
>>   end
>>
>>   class Pencil < ActiveRecord::Base
>> has_many :products, as: :details_info
>>   end
>>
>>   class Notebook < ActiveRecord::Base
>> has_many :products, as: :details_info
>>   end
>> ```
>>
>> Given those models, if you retrieve a list of Product objects you should
>> be able to access each one's `details_info` from the correct tables.
>>
>> If you're still unsure how to proceed, more detail on the specifics of
>> your schema and desired functionality would be useful.
>>
>> --Matt Jones
>>
> --
> 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+unsubscr...@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/a7638d1e-2321-463f-a8dd-2e6b6e4e3421%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@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/CAJri6_tbHN4PKr8E7CqYrVXAPiw_MTVzFS8MU%3DmR%3DVGyv-hjoA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Re: reset association cache for polymorphism

2015-09-20 Thread Oto Iashvili
Thanks a lot for answer, Unfortunately, it is an  site existing for several 
years, so I cant change model, so I really need to be able to change 
"manually" the table_name or to make like a patch forcing to check if 
table_name has been changed and then update association cache 

On Sunday, September 20, 2015 at 10:09:03 PM UTC+2, Matt Jones wrote:
>
>
>
> On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:
>>
>> Hi,
>>
>> I have a product class with category and  a polymorphic attributes 
>> details_info, that depending on category go to one or another table to get 
>> the detail
>>
>>
>> Class Product
>> category
>> details_info
>>
>> end
>>
>> Class DetailsInfo
>> ...
>> end
>>
>> Class Pencil
>>
>>   include DetailsInfo
>> type
>> color
>> end
>>
>> Class Notebook
>>
>>   include DetailsInfo
>> size
>> ...
>> end
>>
>> and to display products i do
>> prds = Products.all
>> prds.each do |pr|
>>
>> if pr.category == 'pencil'
>> DetailsInfo.table_name = 'pencil'
>>
>> else if pr.category == 'notebook'
>>
>> DetailsInfo.table_name = 'notebook'
>> end
>>
>> end
>>
>> (this is just kind of algo I use)
>>
>> All this was working fine but with rails > 4.2 , it doesnt work anymore.
>> The first time pr.details_info will be called, it seems that the table 
>> name is cached somewhere, and then all other products are using the same 
>> table_name. When I check during loop, I can see that DetailsInfo.table_name 
>> is always correct, but still when pr.details_info will be called , it used 
>> the first table_name that was used. I can also see that the method 
>> table_name is called every step of the loop, and it return the good 
>> table_name, but still, the sql request search on bad table.
>>
>> How can I reset that ? Ive tried a lot a thing like 
>> emptying association_cache, also different things 
>> with reflect_on_all_associations, reset_column_information, reload, ...
>>
>>
> As mentioned on rails-core, I'd recommend looking into polymorphic 
> associations for this. Here's a possible example:
> ```
>   class Product < ActiveRecord::Base
> # the products table has two columns relevant here:
> #   * details_info_id, integer
> #   * details_info_type, string
> belongs_to :details_info, polymorphic: true
>   end
>
>   class Pencil < ActiveRecord::Base
> has_many :products, as: :details_info
>   end
>
>   class Notebook < ActiveRecord::Base
> has_many :products, as: :details_info
>   end
> ```
>
> Given those models, if you retrieve a list of Product objects you should 
> be able to access each one's `details_info` from the correct tables.
>
> If you're still unsure how to proceed, more detail on the specifics of 
> your schema and desired functionality would be useful.
>
> --Matt Jones
>

-- 
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+unsubscr...@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/a7638d1e-2321-463f-a8dd-2e6b6e4e3421%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Re: reset association cache for polymorphism

2015-09-20 Thread Matt Jones


On Saturday, 19 September 2015 12:03:47 UTC-4, Oto Iashvili wrote:
>
> Hi,
>
> I have a product class with category and  a polymorphic attributes 
> details_info, that depending on category go to one or another table to get 
> the detail
>
>
> Class Product
> category
> details_info
>
> end
>
> Class DetailsInfo
> ...
> end
>
> Class Pencil
>
>   include DetailsInfo
> type
> color
> end
>
> Class Notebook
>
>   include DetailsInfo
> size
> ...
> end
>
> and to display products i do
> prds = Products.all
> prds.each do |pr|
>
> if pr.category == 'pencil'
> DetailsInfo.table_name = 'pencil'
>
> else if pr.category == 'notebook'
>
> DetailsInfo.table_name = 'notebook'
> end
>
> end
>
> (this is just kind of algo I use)
>
> All this was working fine but with rails > 4.2 , it doesnt work anymore.
> The first time pr.details_info will be called, it seems that the table 
> name is cached somewhere, and then all other products are using the same 
> table_name. When I check during loop, I can see that DetailsInfo.table_name 
> is always correct, but still when pr.details_info will be called , it used 
> the first table_name that was used. I can also see that the method 
> table_name is called every step of the loop, and it return the good 
> table_name, but still, the sql request search on bad table.
>
> How can I reset that ? Ive tried a lot a thing like 
> emptying association_cache, also different things 
> with reflect_on_all_associations, reset_column_information, reload, ...
>
>
As mentioned on rails-core, I'd recommend looking into polymorphic 
associations for this. Here's a possible example:
```
  class Product < ActiveRecord::Base
# the products table has two columns relevant here:
#   * details_info_id, integer
#   * details_info_type, string
belongs_to :details_info, polymorphic: true
  end

  class Pencil < ActiveRecord::Base
has_many :products, as: :details_info
  end

  class Notebook < ActiveRecord::Base
has_many :products, as: :details_info
  end
```

Given those models, if you retrieve a list of Product objects you should be 
able to access each one's `details_info` from the correct tables.

If you're still unsure how to proceed, more detail on the specifics of your 
schema and desired functionality would be useful.

--Matt Jones

-- 
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+unsubscr...@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/5e25fdf7-b166-4e84-956b-645cb62dd3e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.