select single property in hstore field with Rails 4.1 - ruby-on-rails

I am using Postgres 9.3 with Rails 4.1.
Suppose I have a table with a properties hstore colum.
I now want to select a single key inside the hstore column in a scope (or somewhere else..)
What I tried:
Model.select("properties -> 'category'")
What happens: Rails gives me an array like this:
[#<Model id: nil>,
#<Model id: nil>,
#<Model id: nil>,
#<Model id: nil>]
What I want:
[#<Model id: nil, category: 'foo'>,
#<Model id: nil, category: 'bar'>,
#<Model id: nil, category: 'baz'>,
#<Model id: nil, category: 'foo'>]

Try:
=> Model.where("properties #> hstore(?, ?)", 'key', 'value').select(:category)
=> [#<Model id: nil, category: 'foo'>]

Related

rails mongo query based on BSON::ObjectId foreign key

Mongodb and rails
Parent object has_many :children
Child has a parent_id of class BSON::ObjectId
Child.first.parent_id
=> BSON::ObjectId('59031cd92936094d04000d31')
I can not find the Parent with that _id
Parent.where(_id: '59031cd92936094d04000d31').first
=> nil
The Parent object looks like this
=> #<Parent _id: 5a959865c8aedf03c1000007, _type: nil, created_at: nil, updated_at: nil, id: nil, name: nil, description: nil, type: nil, starts_at: nil, children_hash: nil>

Changing a user name in heroku rails console

I would like to edit a supplier name in my heroku database. I'm having trouble accessing the name attribute:
irb(main):015:0> Supplier.where(:name => "Test")
=> #<ActiveRecord::Relation [#<Supplier id: 3070, name: "Test", email: "test#me.com", phone: "555555", website: "http://www.test.co.uk", region_id: 3, category_id: 8, created_at: "2015-02-20 13:28:59", updated_at: "2015-02-20 13:28:59", rating: 0.0, address: nil, facebook_url: nil, twitter_url: nil, google_url: nil, video_url: nil, slug: "test", logo_url: nil, image_one_url: nil, image_two_url: nil, image_three_url: nil, image_four_url: nil, description: nil, reviews_count: 0, source: nil, source_other: nil>]>
irb(main):016:0> _.name
=> "Supplier"
I'm not clear why _.name is resulting in "Supplier" rather than "Test".
Can anyone tell me what I'm missing?
Supplier.where(:name => "Test") returns multiple records. Use
supplier = Supplier.where(:name => "Test").first
supplier.name

Ruby on rails has many through on a concern using a polymorphic association

I have a problem with using has_many through on a concern and from a polymorphic association.
So I have 3 models and concern here:
https://gist.github.com/andreorvalho/5c2f0e3800fbb126df85
my problem is when I create a competition and do:
competition.content_permission_sources I get all the correct values, but when I do
competition. permission_sources I get an empty collection proxy, even though the content_permission_source has a permission_source:
2.1.1 :022 > c.content_permission_sources
=> #<ActiveRecord::Associations::CollectionProxy [#<ContentPermissionSource id: 1, permissible_id: 1, permissible_type: "Competition", permission_source_id: 5, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">, #<ContentPermissionSource id: 2, permissible_id: 1, permissible_type: "Competition", permission_source_id: 4, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">, #<ContentPermissionSource id: 3, permissible_id: 1, permissible_type: "Competition", permission_source_id: 6, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">]>
2.1.1 :023 > c.permission_sources
=> #<ActiveRecord::Associations::CollectionProxy []>
2.1.1 :024 > c.content_permission_sources.first.permission_source
=> #<PermissionSource id: 5, collectable_id: 1, collectable_type: "Site", source_code: 690089, source_name: "fallback", permission_type: "newsletter", country_code: "dk", created_at: "2014-05-26 19:03:01", updated_at: "2014-05-26 19:03:01">
Anybody has an idea of what I am doing wrong and how can I make sure I can access it correctly?

:autosave ignored on has_many relation -- what am I missing?

I have a pair of classes:
class Collection < ActiveRecord::Base
has_many :items, autosave: true
end
class Item < ActiveRecord::Base
belongs_to :collection
end
From the docs:
When :autosave is true all children is saved, no matter whether they are new records:
But when I update an Item and save its parent Collection, the Item's upated attributes don't get saved:
> c = Collection.first
=> #<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">
> i = c.items.first
=> #<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">
> i.name = 'new name'
=> "new name"
> c.save
=> true
> Collection.first.items
=> [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]
So, what am I missing?
I'm using Rails 3.2.5 and Ruby 1.9.2.
So I've done some digging about in the source of ActiveRecord. We can get hold of c's autosave assocations:
> c.class.reflect_on_all_autosave_associations
=> [#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 #macro=:has_many, #name=:items, #options={:autosave=>true, :extend=>[]}, #active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), #plural_name="items", #collection=true, #class_name="Item", #klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), #foreign_key="collection_id", #active_record_primary_key="id", #type=nil>]
I think this illustrates that the association has been set up for autosaving.
We can then get the instance of the association corresponding to c:
> a = c.send :association_instance_get, :items
=> #<ActiveRecord::Associations::HasManyAssociation:0x007fece738e920 #target=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #reflection=#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 #macro=:has_many, #name=:items, #options={:autosave=>true, :extend=>[]}, #active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), #plural_name="items", #collection=true, #class_name="Item", #klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), #foreign_key="collection_id", #active_record_primary_key="id", #type=nil>, #owner=#<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">, #updated=false, #loaded=true, #association_scope=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #proxy=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #stale_state=nil>
We can then find the actual objects that are associated via this association:
> a.target
=> [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]
The object found here does not have update that I'd made earlier.
The problem here is the line
i = c.items.first
This line pulls the correct item from the database, but doesn't attach it to the collection c. It is a distinct ruby object from the object
i = c.items[0]
If you replace the first line with the second your example will work.

.first returning wrong object type

If you look at the four method calls below, Service.first returns a Service object, Salon.first returns a Salon object, etc. But TransactionItem.first returns a Service object. Why could this be?
ruby-1.8.7-p334 :001 > Service.first
=> #<Service id: 147, name: "Fub", salon_id: 2, created_at: "2011-08-10 18:00:07", updated_at: "2011-08-10 18:00:12", price: nil, active: true, archived: true>
ruby-1.8.7-p334 :002 > Salon.first
=> #<Salon id: 1, name: "The Cheeky Strut", created_at: nil, updated_at: nil, address_id: nil, email: nil>
ruby-1.8.7-p334 :003 > Product.first
=> #<Product id: 1, name: "Herbal Essences Shampoo", retail_price: #<BigDecimal:10305f1f0,'0.1E2',9(18)>, wholesale_price: nil, sku: "", salon_id: 2, created_at: "2011-07-08 01:35:48", updated_at: "2011-07-08 01:35:48", archived: false>
ruby-1.8.7-p334 :004 > TransactionItem.first
=> #<Service id: 63, created_at: "2011-08-30 20:05:57", updated_at: "2011-08-30 20:05:57", price: #<BigDecimal:10303eba8,'0.18E2',9(18)>>
ruby-1.8.7-p334 :005 >
This is what my app/models/transaction_item.rb looks like:
class TransactionItem < ActiveRecord::Base
belongs_to :transaction
belongs_to :stylist
end
I blew away the TransactionItem table via a migration, then created a brand new migration to re-create it. That seems to have fixed the problem.

Resources