Ruby on rails - how to convert array - ruby-on-rails

(byebug) content_item.parent_id
*** NoMethodError Exception: undefined method `parent_id' for [#<ContentItemRelationship id: "6186d48b-cdc3-4c83-bea1-3db01d614db8", parent_id: "ac123044-730c-42b8-a220-0b38d6ebe20c", content_item_id: "e0734b6d-76e4-4acf-858f-3c91782f2975", order: nil, created_at: "2022-09-19 13:03:50.430928000 +0000", updated_at: "2022-09-19 13:03:50.430928000 +0000">]:Array
why this error is coming please help me. data bellow :
object content_item contain -
[#<ContentItemRelationship id: "6186d48b-cdc3-4c83-bea1-3db01d614db8", parent_id: "ac123044-730c-42b8-a220-0b38d6ebe20c", content_item_id: "e0734b6d-76e4-4acf-858f-3c91782f2975", order: nil, created_at: "2022-09-19 13:03:50.430928000 +0000", updated_at: "2022-09-19 13:03:50.430928000 +0000">]

as #mechnicov highlighted, here content_item is an array that's when you're getting an error for content_item.parent_id. You need to get the value inside that array using either content_item.first.parent_id or content_item.last.parent_id. This way you'd be able to get parent_id for the content item.

Related

Assigning a hash as association to an object fails if the hash has id, need to understaand why

I have a hash with id which I want to assign as association to an object, I get this error
{"amount"=>nil, "base_currency_amount"=>nil, "currency_id"=>1, "deal_product_id"=>nil,
"expected_close"=>"2021-06-02", "id"=>3, "name"=>"test", "product_id"=>nil}
(byebug) new_record.deal = current_properties["deal"]
*** ActiveRecord::RecordNotFound Exception: Couldn't find LeadDeal with ID=3 for Lead
with ID=
nil
But when I remove the ids from the hash, it works
(byebug) new_record.deal = current_properties["deal"].except("id")
{"amount"=>nil, "base_currency_amount"=>nil, "currency_id"=>1, "deal_product_id"=>nil, "expected_close"=>"2021-06-02", "name"=>"test", "product_id"=>nil}
(byebug) new_record.deal
#<LeadDeal id: nil, account_id: 1623836761, name: "test", amount: nil, expected_close: "2021-06-02", created_at: nil, updated_at: nil, deal_product_id: nil, currency_id: 1, base_currency_amount: nil, product_id: nil>
I thought it's because the new_record is missing id, but even if I add the id it fails with this error
(byebug) new_record.id = 3
3
(byebug) new_record.company = current_properties["company"]
*** ActiveRecord::RecordNotFound Exception: Couldn't find LeadCompany with ID=3 for Lead with ID=3
nil
all the values are present in db, and it works as expected if I just remove ids from the hash, I want to know why this happens

how to make an array into array of arrays in ruby?

I have a query company_in_outs = company.in_outs.where('date >= ? and date <= ?', Date.today.at_beginning_of_month, Date.today.end_of_month) which is returning me <ActiveRecord::AssociationRelation. now i want to make this array into an array of arrays, those are grouped based on the date value.
for example.
#<ActiveRecord::AssociationRelation [#<InOut id: 2806, date: "2016-06-01", created_at: "2016-06-02 07:01:52", updated_at: "2016-06-16 07:43:45", company_id: 1>,#<InOut id: 2806, date: "2016-06-01", created_at: "2016-06-02 07:01:52", updated_at: "2016-06-16 07:43:45", company_id: 1>,#<InOut id: 2806, date: "2016-06-01", created_at: "2016-06-02 07:01:52", updated_at: "2016-06-16 07:43:45", company_id: 1>,#<InOut id: 2806, date: "2016-06-02", created_at: "2016-06-02 07:01:52", updated_at: "2016-06-16 07:43:45", company_id: 1>,#<InOut id: 2806, date: "2016-06-02", created_at: "2016-06-02 07:01:52", updated_at: "2016-06-16 07:43:45", company_id: 1>,#<InOut id: 2806, check_in: "2016-06-24 16:16:00", check_out: "2016-06-25 01:16:00", date: "2016-06-01", created_at: "2016-06-02 07:01:52", updated_at: "2016-06-16 07:43:45", company_id: 1> ]
from this array i want to make an array of arrays, which needs to be grouped based on the date. meaning all records which contains same date needs to be grouped as one array. like this i want to have an array of arrays.
If you already have all the records loaded:
company_in_outs.each_with_object({}) do |record, hash|
(hash[record.date] ||= []) << record
end.values
if you aren't using Rails (ActiveSupport) you should use inject instead of each_with_object
company_in_outs.inject({}) do |hash, record|
(hash[record.date] ||= []) << record
hash
end.values
I think you should be able to done sorting on db site too.
There is actually a group_by method in Array, but instead of returning array of array, it returns an object with key and array of the same key as value.
In your case:
company_in_outs.group_by do |com|
com.date
end
The code above will return an object grouped by date as a key, and array of the same date as value.
You could loop through the returning object which will return key and value, and do whatever you like :)
Hope this helps!

Rails - how to fetch from ActiveRecord object only specific records?

I get this from an ActiveRecord call:
#<ActiveRecord::Associations::CollectionProxy [
#<CarService id: nil, car_id: nil, car_service: 1,
created_at: nil, updated_at: nil, car_type: 0>,
#<CarService id: nil, car_id: nil, car_service: 11,
created_at: nil, updated_at: nil, car_type: 1>]>
Once I get this, I need to filter only records where car_type = "0". How to do that without doing another database call (WHERE car_type = "0")?
Thank you in advance.
EDIT:
this:
car.car_services.select{|key, hash| hash['car_type'] == "1" }
does not work.
just convert your result to an array then filter it like this
result = car.car_services.to_a.select do |e|
e.car_type == "0"
end
You can use scope in CarService model:
scope :type_ones, -> { where(car_type: 1) }
and you can use it like this:
car.car_services.type_ones
If you use enum, it will be better. Because the enum creates to scopes automatically instead of you. And of course it has more features. More about the enum.

Rails 4 to_json produces unexpected Exception nil is not a symbol

I am in the process of upgrading a Rails 3 application to Rails 4. In Rails 3 the json serialization of a hash containing an array of ActiveRecord objects was working correctly; now in Rails 4 it is having unpredictable results.
Here is an example object that fails with TypeError Exception: nil is not a symbol on Rails 4
{1230 =>
[
#<QuestionAnswerResponse response_id: 127, response_set_id: 6, response_group: nil, question_data_export_identifier: "is_co_pi_involved", answer: "No", question_id: 1230, answer_id: 2077, response: "">,
#<QuestionAnswerResponse response_id: 131, response_set_id: 6, response_group: nil, question_data_export_identifier: "is_co_pi_involved", answer: "No", question_id: 1230, answer_id: 2077, response: "">
]
}
Now if I take another similar object; hash containing an array of ActiveRecord objects and run to_json it works on this one...
{1234 =>
[
#<Response id: 1, response_set_id: 2, question_id: 4, answer_id: 2, datetime_value: nil, integer_value: nil, float_value: nil, unit: nil, text_value: nil, string_value: nil, response_other: nil, response_group: nil, created_at: "2014-05-30 21:17:23", updated_at: "2014-05-30 21:17:23", survey_section_id: 1, api_id: "f44b22ba-a93b-477f-8a7f-c5b4566338f0", attach_file_name: nil, attach_content_type: nil, attach_file_size: nil, attach_updated_at: nil>,
#<Response id: 2, response_set_id: 2, question_id: 10, answer_id: 10, datetime_value: nil, integer_value: nil, float_value: nil, unit: nil, text_value: "test", string_value: nil, response_other: nil, response_group: nil, created_at: "2014-05-30 21:17:23", updated_at: "2014-05-30 21:17:23", survey_section_id: 1, api_id: "e7fa8aa2-6e47-4f88-8802-949fdc902a2e", attach_file_name: nil, attach_content_type: nil, attach_file_size: nil, attach_updated_at: nil>
]
}
The view backing my QuestionAnswerResponse model does not have an id column and I was not setting a primary key in the model. In the capacity that I use this model I do not need a primary key; this is a read only view used to more easily access some complex key/value pairings directly instead of through more complex logic.
In Rails 3 this worked fine; in Rails 4 when you access a model without a primary key you end up with an attribute that looks like this in your hash nil => nil
The problem is actually up at the ActiveRecord level, but wasn't actually causing a problem until I attempt to do json serialization; at which point in time an attempt is made to call nil.to_sym which raises the exception.
This seems like a bug in ActiveRecord to me; but for now I have worked around it by manually setting a primary key on my model.

Nil return where it should be exist a value

I'm playing on rails console and did the following steps:
agendamento = Agendamento.last
And got a record that looks like this
#<Agendamento id: 4, sala_id: 2, name: "Delta C", start_at: "2013-10-11 02:00:00", end_at: "2013-10-10 04:00:00", bookingDate: "2013-10-10",
bookingTime: "2000-01-01 23:00:00", bookingDuration: 2, approved: true, usuario_id: nil, created_at: "2013-10-05 02:24:00", updated_at: "2013-
10-05 02:24:00">
when I do agendamento.name I receive:
Delta C
and it works in all values, but not on sala_id, agendamento.sala_id returns
nil
This attribute is acessible in the model, why it is returning nil ?
Environment: Ruby 2 and Rails 4
If sala_id is a foreign key reference, have you tried invoking sala instead? That should give you the related object.

Resources