build method

28 views
Skip to first unread message

fugee ohu

unread,
Oct 31, 2019, 7:42:17 AM10/31/19
to Ruby on Rails: Talk
> @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

Ariel Juodziukynas

unread,
Oct 31, 2019, 2:15:48 PM10/31/19
to rubyonra...@googlegroups.com
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).

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

fugee ohu

unread,
Oct 31, 2019, 9:28:30 PM10/31/19
to Ruby on Rails: Talk


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 (<fuge...@gmail.com>) 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 rubyonra...@googlegroups.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>

Ariel Juodziukynas

unread,
Oct 31, 2019, 9:33:22 PM10/31/19
to rubyonra...@googlegroups.com
@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.

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a80b3961-82df-4943-9e5a-adb764d516b7%40googlegroups.com.

fugee ohu

unread,
Oct 31, 2019, 11:27:05 PM10/31/19
to Ruby on Rails: Talk

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

Ariel Juodziukynas

unread,
Oct 31, 2019, 11:32:22 PM10/31/19
to rubyonra...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b2057f01-80e0-4816-8893-b783e30159f8%40googlegroups.com.

fugee ohu

unread,
Oct 31, 2019, 11:33:08 PM10/31/19
to Ruby on Rails: Talk

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

fugee ohu

unread,
Oct 31, 2019, 11:51:51 PM10/31/19
to Ruby on Rails: Talk

Display item_property attributes in form

Ariel Juodziukynas

unread,
Nov 1, 2019, 12:12:18 AM11/1/19
to rubyonra...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3a2c3dcf-3b21-49c9-8e49-9b2f276d7b6d%40googlegroups.com.

fugee ohu

unread,
Nov 1, 2019, 2:12:21 AM11/1/19
to Ruby on Rails: Talk

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

Walter Lee Davis

unread,
Nov 1, 2019, 8:53:01 AM11/1/19
to rubyonra...@googlegroups.com
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

Message has been deleted
Message has been deleted

fugee ohu

unread,
Nov 2, 2019, 5:21:26 AM11/2/19
to Ruby on Rails: Talk
  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>


Reply all
Reply to author
Forward
0 new messages