last day (16 days later) » 

7:00 PM
0
A: How to .count nested_attribute boolean?

Paweł DawczakYou can update your Result as follows: class Result < ActiveRecord::Base # rest of the code scope :good, -> { where(good: true) } scope :good_count, -> { good.count } end Let's perform some tests in rails console: u = User.create({ user_attributes }) u.results.create(good: true) ...

 
Thanks Pawel for your insightful answer! If your around let's chat. I'll send you a request now. I used your code but it doesn't seem to work, but that might be my fault :)
 
@AnthonyGalli.com - Unfortunately, I don't have much time at the moment (tiny baby crying) - but we can try ~5 min to catch up!
@AnthonyGalli.com - My apologies, but I need to finish for today... Do you have the code somewhere in github maybe? Please add me - pdawczak, so I can try investigating the problem. Or, if you don't like giving the access (which I understand completely!), could you add code of your controller and whole template for view processing the form with partial of quantifieds/_result_fields.html.erb? I think this is the problem with persisting data, rather than reading! Hope to catch up shortly!
 
That's adorable but then again I don't have to listen to him/her. I added the code above. No worries Pawel.
Part of my confusion with your answer is I'm not use to using rails c. I ran your first line but I get an error: 2.1.3 :009 > u = User.create({ user_attributes }) SyntaxError: (irb):9: syntax error, unexpected '}', expecting => u = User.create({ user_attributes })
 
@AnthonyGalli.com - Well, I made a shortcut - I don't know what attributes has your User, but you should replace it with minimal amount of attributes that allow to create a User (if you have any validations). If your User as a mandatory attribute of eg. email, replace this line with u = User.create({ email: "sample@email.com" }). Sorry for confusion!
 
Got it with the next line we get this: 2.1.3 :025 > u.results.create(good: true) ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved
 
7:00 PM
Hi there! Have a sec?
 
Of course
 
This is the thing about validations I mentioned earlier... It appears you need more attributes for User
 
hopefully the baby is okay
 
If you are still in console
try user.errors
 
Yea
got this too
2.1.3 :026 > u.results.count
=> 0
2.1.3 :027 > user.errors
NameError: undefined local variable or method `user' for main:Object
 
7:02 PM
ok then - u.errors ;)
 
2.1.3 :028 > u.errors
=> #<ActiveModel::Errors:0x007fc3a7aca8b0 @base=#<User id: nil, name: nil, email: "sample@email.com", missed_days: nil, missed_levels: nil, provider: nil, uid: nil, oauth_token: nil, oauth_expires_at: nil, created_at: nil, updated_at: nil, password_digest: nil, remember_digest: nil, admin: false, activation_digest: nil, activated: false, activated_at: nil, reset_digest: nil, reset_sent_at: nil>, @messages={:name=>["can't be blank"], :password=>["can't be blank", "is too short (minimum is 6 characters)"]}>
:)
 
ok, so to fix that, try:

u.name = "Sample Name"
u.password = "SuperSecret"
u.save
 
2.1.3 :031 > u.save
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('sample@email.com') LIMIT 1
SQL (36.1ms) INSERT INTO "users" ("email", "name", "password_digest", "created_at", "updated_at", "activation_digest") VALUES (?, ?, ?, ?, ?, ?) [["email", "sample@email.com"], ["name", "Sample Name"], ["password_digest", "$2a$10$W8gfrTyBJyNu5zRPwfVUqeLGGRfFb2Wpa2ejg2mq8CuEAwMwX6Fv2"], ["created_at", "2015-03-19 19:04:10.699498"], ["updated_at", "2015-03-19 19:04:10.699498"], ["activation_digest", "$2a$10$XGdWOolPo525cmG/VqmtaOC
 
Great!
now proceed with rest of my code - u.results.create(good: true)
 
2.1.3 :032 > u.results.create(good: true)
(0.1ms) begin transaction
SQL (18.9ms) INSERT INTO "results" ("good", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["good", "t"], ["user_id", 3], ["created_at", "2015-03-19 19:04:47.905203"], ["updated_at", "2015-03-19 19:04:47.905203"]]
(30.6ms) commit transaction
=> #<Result id: 2, user_id: 3, result_value: nil, date_value: nil, quantified_id: nil, good: true, created_at: "2015-03-19 19:04:47", updated_at: "2015-03-19 19:04:47">
2.1.3 :033 >
 
7:05 PM
u.results.count should be 1
 
2.1.3 :032 > u.results.create(good: true)
(0.1ms) begin transaction
SQL (18.9ms) INSERT INTO "results" ("good", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["good", "t"], ["user_id", 3], ["created_at", "2015-03-19 19:04:47.905203"], ["updated_at", "2015-03-19 19:04:47.905203"]]
(30.6ms) commit transaction
=> #<Result id: 2, user_id: 3, result_value: nil, date_value: nil, quantified_id: nil, good: true, created_at: "2015-03-19 19:04:47", updated_at: "2015-03-19 19:04:47">
2.1.3 :033 > u.results.create(good: false)
 
Great! It means it works!
Do you have any questions to what we have done so far?
 
We created :good results in console
?
then counted it
it works there so we just need to make it work in development
 
indeed!
My question was more about the code if it is clear for you
or, if you would like me to explain anything
 
Your the man. Thanks. Im getting an error now tho when i run rails s
is there a way to clear what we just did in console?
or i could run rake db:migrate
 
7:10 PM
what error did you receive?
 
Routing Error
undefined method `good_results_count' for #<User:0x007f92795c15d0>
one sec sorry
got it nvm
 
great!
ok, so I really need to finish for today!
I'll get back to your code tomorrow, and try investigate
 
haha okay but it still says )
0
okay cool
I'll keep playing around and keep you updated if i figure out the problem
Thanks again!!!
 
do you know what is the ID of user you are testing in web?
 
let me seeee
nope lol
is their a way via console?
 
7:15 PM
in that case, try doing something like <%= current_user.id %> in eg. app/views/layouts/application.html.erb - this is the fastest way ;)
sorry - put this code in layouts/_count.html.erb, and make it <%= @user.id %>
so after refreshing the page, you should see some number - this will be the ID we're looking for
 
Perfect! So, try in console (this will create some data in our *development* database):

u = User.find(2)

u.results.create(good: true)
u.results.create(good: false)
u.results.create(good: true)
 
2.1.3 :039 > u = User.find(2)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
=> #<User id: 2, name: "Anthony Galli", email: "anthonygalli.com@gmail.com", missed_days: nil, missed_levels: nil, provider: nil, uid: nil, oauth_token: nil, oauth_expires_at: nil, created_at: "2015-03-19 16:46:22", updated_at: "2015-03-19 16:47:02", password_digest: "$2a$10$DMYFfkrdJ/.EGjnrsvI8UO8yrx2jHODoQYZYSzmR0gV...", remember_digest: nil, admin: false, activation_digest: "$2a$10$bCCEupP47j5KcvoM/8fzzeOufkJLVHQfTeR2EuZ3/Ud...", activated: true, activated_at: "2015
 
When you finish that, try refreshing your webpage - it should show the number of good results
 
it does!
aweome
 
7:21 PM
Well - now we know for sure, the problem is with saving proper data via forms. I can't help you with this at the moment, but I'll try investigating your code tomorrow. Hope to find the problem, and let you know what to do!
 
Thanks kind sir. Sounds like a plan. Keep living the dream!
 
Sure! hope to find solution and get back to you ASAP!
 
Take your time. Babies first.
Don't feel obligated to answer this now. But could it be because "f" doesn't seem to represent anything in _result_fields.html.erb? Despite that the attributes data are shown in the index. idk
<div class="nested-fields">
<div class="form-group">
<%= f.text_field :result_value, class: 'form-control', placeholder: 'Enter Result' %>

<br/>

<%= f.date_select :date_value, :order => [:month, :day, :year], :with_css_classes => true, :class => "modular-date-field" %>

<b><%= link_to_remove_association "Remove Result", f %></b>

<div class="america3">

<input type="checkbox">
<label> Good: </label>
<%= f.check_box :good %>
</input>

</div>
</div>
</div>
I tried adding <%= simple_form_for(@result) do |f| %> #codehere <% end %>
but I got an error: undefined method model_name' for nil:NilClass`
 

  last day (16 days later) »