On Thursday, September 26, 2019 at 5:03:00 PM UTC-4, Ariel Juodziukynas 
wrote:
>
> You were asking a way to store different values, I thought you wanted to 
> preserve the type (so you can distinguish 1 from "1" when you read the 
> record).
>
> If you think strings are enough for your requirements just use a string 
> column, if you want to use one column to store values but preserve the 
> original type you have to serialize the value somehow (that's the is_a?... 
> when serializing and the case when casting).
>
> Another option is to use two columns: one for the stringified value and 
> one for the original type so you can parse that again.
>
> El jue., 26 sept. 2019 a las 17:57, fugee ohu (<[email protected] 
> <javascript:>>) escribió:
>
>>
>>
>> On Thursday, September 26, 2019 at 4:32:10 PM UTC-4, Ariel Juodziukynas 
>> wrote:
>>>
>>> The easiest but too big is a text columns so you can use 
>>> activerecord' serialization of attributes to save anything you want on a 
>>> text field and activerecord will handle casting.
>>>
>>> You could use a string column if you know your values won't be too big 
>>> (VARCHAR(255)) and you know what types you want to accept and handle 
>>> serialization yourself like:
>>>
>>>     def value=(something)
>>>       val = if value.is_a?(Integer)
>>>               "integer:#{something}"
>>>             elsif value.is_a?(String)
>>>               "string:#{something}"
>>>             elsif value.is_a?(Date)
>>>               "date:#{something}
>>>             # etc...
>>>       write_attribute(:value, val)
>>>     end
>>>
>>>     def value
>>>       type, val = read_attribute(:value).split(':')
>>>       case type
>>>       when "integer" then val.to_i
>>>       when "string" then val
>>>       when "date" then Date.parse(val)
>>>       etc...
>>>     end
>>>            
>>>
>>> El jue., 26 sept. 2019 a las 17:17, fugee ohu (<[email protected]>) 
>>> escribió:
>>>
>>>>
>>>>
>>>> On Monday, September 16, 2019 at 4:31:29 PM UTC-4, Ariel Juodziukynas 
>>>> wrote:
>>>>>
>>>>> Personally, I would do this:
>>>>>
>>>>> auctions table
>>>>> (with the basic shared information of all auctions and an "auction 
>>>>> type"))
>>>>>
>>>>> properties table
>>>>> property_name (like network, carrier, publisher, etc)
>>>>> auction_type (like cellphone, book, etc)
>>>>>
>>>>> auctions_properties
>>>>> auction_id
>>>>> property_id
>>>>> value
>>>>>
>>>>> That way you can have any number of auction types with any number of 
>>>>> specific properties with just 3 tables.
>>>>>
>>>>> Note that the "value" column would be some string variation (VARCHAR, 
>>>>> CHAR, TEXT, etc) depending on your needs, maybe you want to redesign it a 
>>>>> little if you want to store different types. Like if you want to store an 
>>>>> integer (and retrieve an integer) you'll have to save the original type 
>>>>> and 
>>>>> reparse it (you could use serialization but that requires a TEXT column 
>>>>> and 
>>>>> maybe you can't use that many space)
>>>>>
>>>>> El lun., 16 sept. 2019 a las 17:19, fugee ohu (<[email protected]>) 
>>>>> escribió:
>>>>>
>>>>>> I was looking at some auction projects that use a single listings 
>>>>>> table for all auctions but I know on auction sites the form will be 
>>>>>> different for different types of items like if you're selling a cell 
>>>>>> phone 
>>>>>> there'll be a form field for network, carrier, whatever and if you're 
>>>>>> selling a book there'll be form fields for publisher, year of 
>>>>>> publication,  
>>>>>> so they would have separate tables I assume for books, cell phones, etc? 
>>>>>> Then how would they treat them all as one?
>>>>>>
>>>>>> -- 
>>>>>> 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 [email protected].
>>>>>> To view this discussion on the web visit 
>>>>>> https://groups.google.com/d/msgid/rubyonrails-talk/512f8b73-e3a4-4e74-95e7-4281e7b54821%40googlegroups.com
>>>>>>  
>>>>>> <https://groups.google.com/d/msgid/rubyonrails-talk/512f8b73-e3a4-4e74-95e7-4281e7b54821%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>>
>>>>>
>>>> What data type do you suggest for value column 
>>>>
>>>> -- 
>>>> 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 [email protected].
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/rubyonrails-talk/e74aaf6b-b9e1-4847-a6d3-3120829fa3bc%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/rubyonrails-talk/e74aaf6b-b9e1-4847-a6d3-3120829fa3bc%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>
>>  If I'm using a string column then whatever value is put in it is a 
>> string, so I don't understand all these "'if value.is_a?" conditions
>>
>> -- 
>> 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 [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rubyonrails-talk/93726aa7-2667-4f3b-b27c-6d19d5e5f368%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/rubyonrails-talk/93726aa7-2667-4f3b-b27c-6d19d5e5f368%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
i=Item.first
i.item_properties
NameError: uninitialized constant Item::ItemProperties

class Item < ApplicationRecord
 has_many :boxes
 has_many :item_pictures
 has_many :pictures, through: :item_pictures
 has_many :item_item_properties, primary_key: 'item_type', foreign_key: 
'item_type'
 has_many :item_properties, through: :item_item_properties
 ITEM_TYPES =  ['General', 'Book', 'Record', 'Magazine', 'Pez dispenser', 
'CD', 'VHS casette']
 validates :item_type, inclusion: ITEM_TYPES
 attr_accessor "Year"
 
end

class ItemItemProperty < ApplicationRecord
 belongs_to :items, primary_key: 'item_type', foreign_key: 'item_type'
 belongs_to :item_properties, primary_key: 'item_type', foreign_key: 
'item_type'
end

class ItemProperty < ApplicationRecord
 has_many :item_item_properties, primary_key: 'item_type', foreign_key: 
'item_type'
 has_many :items, through: :item_item_properties
end
 

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/fb472c34-e7ab-4274-945d-03233a21e77c%40googlegroups.com.

Reply via email to