build method

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

build method

fugee ohu
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

Ariel Juodziukynas
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcC%3Djx6MabXASiB2DXgNMeWdw0TMo%2B%3D979RgfHnoN-rH_Q%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="W9ZDOu9sAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="W9ZDOu9sAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual values

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5ec7f561-3926-43bb-9883-0ac7d5f36642%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu
In reply to this post by Ariel Juodziukynas


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="W9ZDOu9sAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="W9ZDOu9sAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

Ariel Juodziukynas
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcAxtZ4PK3yFBT9EZ%3DeB241Ub488xyePjQnozOkbXXyo0Q%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu


On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="noPGYPeaBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="noPGYPeaBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

Could I directly access key, value without pluck or otherwise like
 @item.item_item_properties.each do |key, value|
item_property_id

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

Ariel Juodziukynas
You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.

El vie., 1 nov. 2019 a las 0:27, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

Could I directly access key, value without pluck or otherwise like
 @item.item_item_properties.each do |key, value|
item_property_id

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcCGOC%3DindJPMT0xJGj9EmJ1c7tMLTpP00Vb1nX8wBQuUQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu
In reply to this post by Ariel Juodziukynas


On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="noPGYPeaBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="noPGYPeaBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

 2.3.3 :006 > @item.item_item_properties.pluck(:item_property_id)
 => []

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6d62e08d-57b2-4584-9271-e89aa6ca17be%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu
In reply to this post by Ariel Juodziukynas


On Thursday, October 31, 2019 at 11:32:22 PM UTC-4, Ariel Juodziukynas wrote:
You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.

El vie., 1 nov. 2019 a las 0:27, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="SFjBzXWhBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:


On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

Could I directly access key, value without pluck or otherwise like
 @item.item_item_properties.each do |key, value|
item_property_id

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="SFjBzXWhBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.

Display item_property attributes in form

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

Ariel Juodziukynas
If you have:
item has_many item_item_properties
item_item_property belongs_to item_property

then you can add a relationship on the item model

has_many :item_properties, through: :item_item_properties

Then you'll be able to call @item.item_properties to loop through the ItemProperty objects associated to that Item object.

Now, you say @item.item_item_properties.pluck(:item_property_id) returns an empty array, that's because the Item does not have associated properties and pluck does an SQL.

El vie., 1 nov. 2019 a las 0:52, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 11:32:22 PM UTC-4, Ariel Juodziukynas wrote:
You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.

El vie., 1 nov. 2019 a las 0:27, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

Could I directly access key, value without pluck or otherwise like
 @item.item_item_properties.each do |key, value|
item_property_id

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.

Display item_property attributes in form

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcBL5tp8T2_TG5f65OjDRUO03CeW08T3GHAVwMeNq%2BETGg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu


On Friday, November 1, 2019 at 12:12:18 AM UTC-4, Ariel Juodziukynas wrote:
If you have:
item has_many item_item_properties
item_item_property belongs_to item_property

then you can add a relationship on the item model

has_many :item_properties, through: :item_item_properties

Then you'll be able to call @item.item_properties to loop through the ItemProperty objects associated to that Item object.

Now, you say @item.item_item_properties.pluck(:item_property_id) returns an empty array, that's because the Item does not have associated properties and pluck does an SQL.

El vie., 1 nov. 2019 a las 0:52, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Izest6OjBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:


On Thursday, October 31, 2019 at 11:32:22 PM UTC-4, Ariel Juodziukynas wrote:
You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.

El vie., 1 nov. 2019 a las 0:27, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
@item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.

If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.

El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

How do I reference individual columns?
 2.3.3 :006 > @item.item_item_properties.item_property_id
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

Could I directly access key, value without pluck or otherwise like
 @item.item_item_properties.each do |key, value|
item_property_id

--
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 [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.

Display item_property attributes in form

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Izest6OjBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.

There are no database records yet, we know, the object was instantiated by the build method but so far I don't see how to read it's data for my views

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7e40c90a-3a1b-4844-8c19-d1b02eef347c%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

Walter Lee Davis


> On Nov 1, 2019, at 2:12 AM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Friday, November 1, 2019 at 12:12:18 AM UTC-4, Ariel Juodziukynas wrote:
> If you have:
> item has_many item_item_properties
> item_item_property belongs_to item_property
>
> then you can add a relationship on the item model
>
> has_many :item_properties, through: :item_item_properties
>
> Then you'll be able to call @item.item_properties to loop through the ItemProperty objects associated to that Item object.
>
> Now, you say @item.item_item_properties.pluck(:item_property_id) returns an empty array, that's because the Item does not have associated properties and pluck does an SQL.
>
> El vie., 1 nov. 2019 a las 0:52, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 11:32:22 PM UTC-4, Ariel Juodziukynas wrote:
> You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.
>
> El vie., 1 nov. 2019 a las 0:27, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
> @item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.
>
> If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.
>
> El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
> Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).
>
> El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> > @item=Item.new
> > @item.item_item_properties.build
> > @item.item_item_properties.count
>  => 0
>
> Why doesn't my @item object have any item_item_properties after build method
>
> --
> 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 [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.
>
> How do I reference individual columns?
>  2.3.3 :006 > @item.item_item_properties.item_property_id
> NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>
>
>
> --
> 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 [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.
>
> Could I directly access key, value without pluck or otherwise like
>  @item.item_item_properties.each do |key, value|
> item_property_id
>
> --
> 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 [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.
>
> Display item_property attributes in form
>
> --
> 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 [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.
>
> There are no database records yet, we know, the object was instantiated by the build method but so far I don't see how to read it's data for my views
>

You're trying to build a nested form, with the possibility of adding N children to the parent form. When you build a new child object in memory, you need to create a form for that object dynamically. There's a Railscast about this idea, which you may find useful: http://media.railscasts.com/assets/episodes/videos/196-nested-model-form-revised.mp4

Here's a link to the article about that screencast: http://railscasts.com/episodes/196-nested-model-form-revised

Please note that these videos are very old, and the specific details may differ from modern Rails practice, but the ideas underlying them are sound, and still hold up today.

Walter

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1AABE975-5E86-4C32-9F84-B91264663D8A%40wdstudio.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu


On Friday, November 1, 2019 at 8:53:01 AM UTC-4, Walter Lee Davis wrote:


> On Nov 1, 2019, at 2:12 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="RwEk-w3ABAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Friday, November 1, 2019 at 12:12:18 AM UTC-4, Ariel Juodziukynas wrote:
> If you have:
> item has_many item_item_properties
> item_item_property belongs_to item_property
>
> then you can add a relationship on the item model
>
> has_many :item_properties, through: :item_item_properties
>
> Then you'll be able to call @item.item_properties to loop through the ItemProperty objects associated to that Item object.
>
> Now, you say @item.item_item_properties.pluck(:item_property_id) returns an empty array, that's because the Item does not have associated properties and pluck does an SQL.
>
> El vie., 1 nov. 2019 a las 0:52, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 11:32:22 PM UTC-4, Ariel Juodziukynas wrote:
> You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.
>
> El vie., 1 nov. 2019 a las 0:27, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
> @item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.
>
> If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.
>
> El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
> Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).
>
> El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> > @item=Item.new
> > @item.item_item_properties.build
> > @item.item_item_properties.count
>  => 0
>
> Why doesn't my @item object have any item_item_properties after build method
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.
>
> How do I reference individual columns?
>  2.3.3 :006 > @item.item_item_properties.item_property_id
> NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>
>
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.
>
> Could I directly access key, value without pluck or otherwise like
>  @item.item_item_properties.each do |key, value|
> item_property_id
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.
>
> Display item_property attributes in form
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.
>
> There are no database records yet, we know, the object was instantiated by the build method but so far I don't see how to read it's data for my views
>

You're trying to build a nested form, with the possibility of adding N children to the parent form. When you build a new child object in memory, you need to create a form for that object dynamically. There's a Railscast about this idea, which you may find useful: <a href="http://media.railscasts.com/assets/episodes/videos/196-nested-model-form-revised.mp4" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmedia.railscasts.com%2Fassets%2Fepisodes%2Fvideos%2F196-nested-model-form-revised.mp4\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFNdnr8N9qgu8NLLNAHHqwQ1qeFIg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmedia.railscasts.com%2Fassets%2Fepisodes%2Fvideos%2F196-nested-model-form-revised.mp4\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFNdnr8N9qgu8NLLNAHHqwQ1qeFIg&#39;;return true;">http://media.railscasts.com/assets/episodes/videos/196-nested-model-form-revised.mp4

Here's a link to the article about that screencast: <a href="http://railscasts.com/episodes/196-nested-model-form-revised" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frailscasts.com%2Fepisodes%2F196-nested-model-form-revised\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFe2uA2Jm5soa2PYjqVQh2D1Y0j3w&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frailscasts.com%2Fepisodes%2F196-nested-model-form-revised\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFe2uA2Jm5soa2PYjqVQh2D1Y0j3w&#39;;return true;">http://railscasts.com/episodes/196-nested-model-form-revised

Please note that these videos are very old, and the specific details may differ from modern Rails practice, but the ideas underlying them are sound, and still hold up today.

Walter


I was able to display @item.item_properties.size in my view I'm just having trouble pulling up individual elements

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a9e3b23f-8997-4bdf-bdb0-4564b65d325d%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu
In reply to this post by Ariel Juodziukynas


On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).

El jue., 31 oct. 2019 a las 8:42, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="W9ZDOu9sAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:
> @item=Item.new
> @item.item_item_properties.build
> @item.item_item_properties.count
 => 0

Why doesn't my @item object have any item_item_properties after build method

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="W9ZDOu9sAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.

What other info can I get from the initialized association

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c8b4533e-7082-4893-be96-123af19807f3%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: build method

fugee ohu
In reply to this post by Walter Lee Davis


On Friday, November 1, 2019 at 8:53:01 AM UTC-4, Walter Lee Davis wrote:


> On Nov 1, 2019, at 2:12 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="RwEk-w3ABAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Friday, November 1, 2019 at 12:12:18 AM UTC-4, Ariel Juodziukynas wrote:
> If you have:
> item has_many item_item_properties
> item_item_property belongs_to item_property
>
> then you can add a relationship on the item model
>
> has_many :item_properties, through: :item_item_properties
>
> Then you'll be able to call @item.item_properties to loop through the ItemProperty objects associated to that Item object.
>
> Now, you say @item.item_item_properties.pluck(:item_property_id) returns an empty array, that's because the Item does not have associated properties and pluck does an SQL.
>
> El vie., 1 nov. 2019 a las 0:52, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 11:32:22 PM UTC-4, Ariel Juodziukynas wrote:
> You can do @item.item_item_properties.each do |item_item_property| and handle that object inside the loop, I'm not sure why would you expect that item_item_properties relationship to return key, value pairs. I don't understand what you want to achieve, maybe you are trying to do it on the wrong path.
>
> El vie., 1 nov. 2019 a las 0:27, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 9:33:22 PM UTC-4, Ariel Juodziukynas wrote:
> @item.item_item_properties returns a collection of objects, you are calling a method on something array-like, it makes no sense.
>
> If you want to get all the item_property_ids you can do @item.item_item_properties.pluck(:item_property_id) for example. I'm not sure what you want to achieve.
>
> El jue., 31 oct. 2019 a las 22:29, fugee ohu (<[hidden email]>) escribió:
>
>
> On Thursday, October 31, 2019 at 2:15:48 PM UTC-4, Ariel Juodziukynas wrote:
> Use `.size` instead of `.count`. "count" does a database query and since you only built the element the COUNT db query will return 0. "size" knows what to do if the association is already initialized so it will return "1" in your case (and if it's not already loaded it will run a COUNT db query).
>
> El jue., 31 oct. 2019 a las 8:42, fugee ohu (<[hidden email]>) escribió:
> > @item=Item.new
> > @item.item_item_properties.build
> > @item.item_item_properties.count
>  => 0
>
> Why doesn't my @item object have any item_item_properties after build method
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/161b90ed-b6b1-4d4d-9f67-fb70f0463621%40googlegroups.com.
>
> How do I reference individual columns?
>  2.3.3 :006 > @item.item_item_properties.item_property_id
> NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0055d4167fd628>
>
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.
>
> Could I directly access key, value without pluck or otherwise like
>  @item.item_item_properties.each do |key, value|
> item_property_id
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.
>
> Display item_property attributes in form
>
> --
> 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 [hidden email].
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.
>
> There are no database records yet, we know, the object was instantiated by the build method but so far I don't see how to read it's data for my views
>

You're trying to build a nested form, with the possibility of adding N children to the parent form. When you build a new child object in memory, you need to create a form for that object dynamically. There's a Railscast about this idea, which you may find useful: <a href="http://media.railscasts.com/assets/episodes/videos/196-nested-model-form-revised.mp4" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmedia.railscasts.com%2Fassets%2Fepisodes%2Fvideos%2F196-nested-model-form-revised.mp4\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFNdnr8N9qgu8NLLNAHHqwQ1qeFIg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmedia.railscasts.com%2Fassets%2Fepisodes%2Fvideos%2F196-nested-model-form-revised.mp4\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFNdnr8N9qgu8NLLNAHHqwQ1qeFIg&#39;;return true;">http://media.railscasts.com/assets/episodes/videos/196-nested-model-form-revised.mp4

Here's a link to the article about that screencast: <a href="http://railscasts.com/episodes/196-nested-model-form-revised" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frailscasts.com%2Fepisodes%2F196-nested-model-form-revised\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFe2uA2Jm5soa2PYjqVQh2D1Y0j3w&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frailscasts.com%2Fepisodes%2F196-nested-model-form-revised\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFe2uA2Jm5soa2PYjqVQh2D1Y0j3w&#39;;return true;">http://railscasts.com/episodes/196-nested-model-form-revised

Please note that these videos are very old, and the specific details may differ from modern Rails practice, but the ideas underlying them are sound, and still hold up today.

Walter


  def new
        @item = Item.new(item_params)
        ItemProperty.where(item_type: params[:item][:item_type]).each do |ip|
            @item.item_item_properties.build(item_property_id: ip.id)
        end
end

Then trying to reference @item.item_item_properties.item_property_id returns error:
NoMethodError: undefined method `item_property_id' for #<ItemItemProperty::ActiveRecord_Associations_CollectionProxy:0x0056269f53ab08>


--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/adbe5063-5278-4680-b50f-27ecf4e5f339%40googlegroups.com.