« first day (1259 days earlier)      last day (1996 days later) » 

user5870134
2:00 AM
I just woke up from a nap, feeling refreshed.
 
user5870134
Good morning, everyone!
 
Good morning.
 
 
4 hours later…
6:11 AM
23 hours ago, by Ganesh Kumar
i'm not able to test my api call using webmock.. if anybody used webmock before help me to overcome this problem
hi guys..
need help with the above one
 
 
4 hours later…
 
1 hour later…
11:04 AM
good morning people
 
morning
 
11:20 AM
Tempted to reply to Wayne's question with an answer that simply says "no" & nothing else :)
the other part of me wants to pull out my c & assembly books to compare against the source in sort...but I can't even write tests regularly, so probably need to spend more time with them (I was told ruby can pull in faster libraries from them)
 
11:41 AM
0
Q: Stub Request is not working

Ganesh KumarI have been stuck in this problem for the past two days. This webmock stub request is not working while running the test. Here is the controller. class AdvisorDashboardController < ApplicationController load_and_authorize_resource :class => AdvisorDashboardController require 'Numeric.rb' i...

 
12:27 PM
@GaneshKumar ... I'm too new to rspec to help & never done webmock ... have you searched the github & official documentation for rspec & webmock yet? Your other option is isolated by removing everything extra.
 
Soooo
hey
some simple code is annoying me
    @store = Store.where(key:params[:key])

    @user = User.find(@store.user_id)
undefined method `user_id' for #<Store::ActiveRecord_Relation:0x007ff397779c90>

How do i access the user_id attribute? because when i debug @store i can see it
 
@Mirv oh thanks for your reply mate.. no problem
@thageekboy you there?
 
yup
here,
trying to see what other way i can access that value.
 
@store is an array
you cant access that directly.. instead you can try like this
 
so how do you access an array values in rails, soory, im learning rails
 
12:39 PM
@store[0].user_id
if it has only one item in that array
 
anyway to convert it? i guess this happens when i use where instead of find?
 
did you check @store? how many items does it contain?
 
Just one
key is unique
they column key is unique
 
@user = User.find(@store[0].user_id)
try this
 
Hey thanks, it works....
Suppose i was to use
Store.find(storeid)

then i do

User.find(@store.user_id)

it would work
.find and .where give different data types?
 
12:50 PM
@thageekboy ...odd, did the normal `@store = Store.where(params[:<whateverKey>]) not work? Normally it's a matter of just digging thru to grab the key from the activerecord in the controller & then normal array operations after ... but I can definitely see using the grab like that if you're only using that one entry...
 
This works
@store = Store.find(1)

    @user = User.find(@store.user_id)
This doesnt
@store = Store.where(key:params[:key])

@user = User.find(@store.user_id)
until i use the method you have shown me
@user = User.find(@store[0].user_id)
i guess its the .find and .where methods giving different output formats
 
Key:params is nothing i've ever heard of ..
 
key is the column
basically find store where key = params[:key]
 
the column in the controller? cause they don't have columns?
Can you link your private method with params in it?
(that's what you should be calling against - eliminating need for key)
oh....maybe I'm saying this wrong
Side note, .where is a pretty wrapper for directly writing sql queries, so yes, very different languages.
 
Yeah, because if i use .where, it gives me an array which i have to access like @store[0].user_id
if i use .find, i use @store.user_id
 
1:26 PM
Ok from your original post today ...your issue is you weren't using the key you setup & skipping to the :user_id before you did :key ... @store = Store.where(key:params[:key]), it fails because you have key:params[:key] ... you just stuck the user_id into the key named "key" .... the next line down should be User.find(@store[:key][:user_id]) ... I only bring this up as [0][:user_id]is brittle, where as [:key][:user_id] is less
(but that's a guess)
 
Good morning everyone!
 
alternatively, you could also do @store.first.user_id instead of @store[:key][:user_id] in the .find
hi mar-andre!
 
@Mirv Yes this worked too
@Marc-Andre Good Morning! Its 1637hrs here lol
Im sleepy as hell, i had a heavy lunch
 
@thageekboy Yeah in some other room we use Monking to refer to Good [whatever you have a your timezone]. Don't know what would work here
 
in other places, we also use, "what^ f00?"
(whatup fool)
@thageekboy I'd be tempted to keep it with the [:key][:user_id] as they will always seek it out where as the other two options will become complicated as the code base changes
 
1:42 PM
Haha interesting, well i pretty much use this room only.... But good to know! @Mirv
what^ f00! :D
@Mirv At this point i have a few objectives to hit, its not exactly standard but this is what i have
@sto = Store.where(key:params[:key])
@store = @sto[0]
@user = User.find(@store.user_id)
How would you improve that?
@store =Store.where(key:params[:key])[0] works too
 
very thorough....hadn't considered that one!
 
@Mirv As for this, you're a bit off, all i did was use the key column to find the record instead of id, because the controller will recieve a key.. assume its an API key
 
@sto = Store.where(key: params[:key]).first is bit more readable & rails
 
Yeah, thats why i did it. Its readable
 
I'm just learning with you some too - if you've got stuff to d, i've got boring stuff too :(
 
1:50 PM
Hahaha well i am learning rails while building an app... although i am past the frustrations now.. This room comes to my aid when i am stuck... Thank stackoverflow! I am an ex-php coder
 
say...how are you getting multiline in the text?
ditto, symfony2 was my last one before laravel
 
@Mirv shift + Enter :D
I have been doing code igniter for some 3 years, i was to do laravel but i thought i might as well move to the real deal rails
 
`@sto = Store.where(key: params[:key]).first
@user = User.find(store)`
Will actually work to....as rails pulls the user_id with magic
I'm learning pheonix for backend once I finish this & emberjs
 
@Mirv Yup, works perfectly too, i just tried it
 
@thageekboy ....small question....if all you want is the ID, why not just pluck it which is WAY faster & returns single entry so you don't have to jump around with array?
sorry - not pluck - but I know there's a way of not using an array for a single object like what you're doing
 
1:56 PM
You see, all i have at first is the key.. with the key i can find the store, and with the store i can find the user who owns the store
 
OH!
that's db side stuff
We just need to patch your .where query up some!
don't waste time with rails trying to solve that usecase when the sql is faster exponentialy
 
If i was receiving the store id first, this wouldn't be a problem. i would use @store = Store.find(params[:id])

then @user = User.find(@store.user_id)
How do i patch the .where query?
 
So sql is easier WAY more transparent than rails ... we just keep sticking params in there and let it select out the matching column
let me get back to you in 5 minutes have something have to do
 
2:30 PM
@thageekboy assuming you have ActiveRecord relations setup and users have one store you could do something like User.joins(:store).where("stores.key = ?", params[:key]).first
 
@Jared A User has many stores
 
if you pluralize that join it will still work then :D
 
This seems simpler ?

@sto = Store.where(key: params[:key]).first
 
But do you care about the store, or the user?
 
@thageekboy Do you have a has_many or has_many through relationship between store & user? if so ... @user = @store.user, to make it an object @user = User.find(@store.user)
 
2:34 PM
The joins query make it so it is a single query to the database. It would be more noticeable performance wise if they were complex queries, but still is better to do one than many if possible.
 
i agree with jared on the join being fastest - but if you can single line it (@store already exists), rails will write the query for you much better than we can
 
Also once you have @store you can just do @store.user
 
@Jared will that return the id or the object?
 
Object
 
did not know! thanks
 
2:39 PM
if you specifically want the id you can call @store.user_id
 
yay for rails magic!
 
Yea, ActiveRecord handles 95% of the heavy lifting, but it leaves 10% to weird issues.
 
@Jared I care about both store and user... And the relationships are set, a use has_many :store and stores belongs_to user
 
Yea, so you can just do @store = Store.find_by(key: params[:key]).includes(:user)
 
Thats where the problem came in Jared, i couldnt do @store.user as i always do... So we concluded .find and .where return different data types.. with

.find @store.user works

.where @store[0].user is what works
 
2:41 PM
i hate AR...I know all the arguments for it & have never seen any of the supposed benefits which would out weight 3.5 months of trouble shooting around it's magic in the past 6 months....
 
@Jared Let me try this @Jared
 
That includes just tells ActiveRecord to eagerly load the user so when you do @store.user it won't go back to the database
 
@store = Store.find_by(key: params[:key]).includes(:user)
 
so its both :D
 
undefined method `includes' for #<Store:0x007f482cdafd60>
i saw somewhere find_by is deprecated, i should just use Store.where(key:params[:key])
 
2:46 PM
@thageekboy Store.includes(:user).find_by(key: params[:key])
 
Order would do it... I think its time for coffee #2 -.-
@thageekboy find_by_key(params[:key]) is depricated in favor of the find_by(foo: ?) syntax
 
find_by is probably not deprecate - stackoverflow.com/questions/32792326/is-rails-find-deprecated - I also checked the rails api & nothing mentioned there either...incidently .where is favored over find_by, but find seems to be ok for single returns (which as far as I understand is what you're doing)
 
Jared does it again, this worked like a charm

Store.includes(:user).find_by(key: params[:key])
Now to go back and actually read your explanation of includes :D :D
 
If you want someone smart to explain ActiveRecord Includes
 
Ok, but why is this so complicated Jared? I've never seen so much work to pull the userid from a has_many relationship?
 
2:56 PM
Why do we use includes instead of just Store.find_by(...).user ?
 
@user = Table.find(params[:id]).user should do it right?
or is that two queries?
 
the find is 1, the user is another
 
@Jared If you don't includes, I guess it would still trigger a second query
 
Well the first statement in the documentation explained everything.. actually its quite useful

"Specify relationships to be included in the result set"
 
ouch....I'm going to have to rewrite a ton of code
 
2:57 PM
If you use the includes its telling rails that you'll want the User object and its attributes so it will "eager load" that object
That way when you do .user.first_name it will already have it in memory
 
Oh you were explaining , I thought you were asking the question for real. My bad ;)
 
roger
 
Hehe, yea, I was trying to determine the question to answer ;)
Well off to a demo, time to give them some constructive criticism ^_^
 
3:10 PM
hello
 
hi Mak
 
I have a question which might be more discussion-y than is usually allowed on the main bit of stackoverflow so I'll ask here
I'm making a chat application in rails. Is it advisable to use Jobs to handle creating/deleting messages etc.?
it might make the app quicker, but also it pulls out some of the code that perhaps should be in the controllers and puts that code into the Job instead
 
I'd think CRUD would say keep it in the controller, unless it's a maintenance type thing where you're purging records which are super old
 
ok cool
thank you
 
but i see no issue with a service object having all the code & controller calling it & then a wrapper class in jobs extending that same code if you're doing both
(i'm not a super expert by anymeans)
 
3:15 PM
alright thanks for the idea
I'm pretty new to ruby and rails so it's a bit of a learning process
 
It doesn't work for everyone, but basically the maker of rails says, when in question - spin off a new controller & allow the built in CRUD to save you time initially...as in the long run it's easier to understand than some new guy getting used to your custom service objects
 
ok that's some good advice. Thanks very much
understandability of my code is an important consideration since it's open-source
 
Good morning, all.
 
Good morning
 
morn
 
3:27 PM
hiyas Wayne
 
@Mirv Thanks for the link. I look forward to reading it.
When I left work last night, I figured out a clever hack to fix this bug. Got to work this morning, started writing a test for the clever hack, and realized that all I really need to do is to delete one line of code. Yay for tests!
 
w00t
yw Wayne .... question of my own....I've got this gravatar thing....when I'm testing it should i just test that the link works or that the image loads? I don't want to use capybara on this project, so just looking to use assertions....thoughts?
 
I don't know enough.
What kind of test are you writing? A Rails controller/model/view/etc test?
 
@Mirv have you looked into VCR ?
 
integration - against controller mostly though this one will probably have to check html div exists with class of the image
@Jared is it phantom dependent?
 
3:40 PM
VCR does not run a headless browser. It hooks into Ruby's library to intercept HTTP traffic, allowing recording and replay of external traffic. I've used it, it's good.
 
No, it just captures any HTTP request and saves the whole response in a 'cassette' (just a yml file) and replays from that yml whenever you run your test suite
 
wow
bit over my head, but sounds kind of cool
 
The other thing you can do is to hide the external entity behind a library that you can mock in your tests. Prevent your tests from ever talking to anything. Usually you would then have a separate test for the library, possibly using VCR.
 
how does it apply to checking an img loads each time?
 
Basically call gravatar as usual and it will mock up the external call on all future tests
 
3:42 PM
what i want to know is when the gravator doesn't display
...for this one, I don't want the mock
 
Isn't the gravitar loaded on the client side?
 
good question - but there's a gravatar_for helper we call from rails side
 
I'll bet that all that helper is doing is putting some HTML (and maybe JS) on the page.
 
def gravatar_for(user, options = { size: 80})
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.first_name, class: "gravatar")
end
aye
 
For a test that does not involve a headless browser, the best you can do is test that the HTML got emitted. Otherwise, your test will need to use a headless browser.
 
3:44 PM
I wrapped it in a bit of code so if user isn't set - it doesn't display anything - now I want to test that
 
What bit of code?
 
so basically assert_match <html_tag> <"name of class for gravatar"> type?
There's a helper setup which does call_gravatar unless user.nil?, then call gravatar outputs the <h5>User: <%= user %> next line is gravatar_for .... next line is some closing of the html
so theoretically if everything from my input is good & gravatar service working - all I do is test the <h5> with an added class to make simpler for testing
 
Yeah, something like that assert_match. That will tightly couple the test to the HTML, so I might create another helper method called, e.g., loads_gravatar?. Put that method right next to the garvatar_for function so that they live together: Because if one changes, the other will have to. Then have the test call loads_gravatar? to check the page.
 
right :)
unfortunately, we play ping pong over the current_user, the set_user etc & gravatar is the latest one that keeps changing back & forth
 
4:31 PM
random question - i've read like 50+ articles on where things go...so I've narrowed down the location of a view partial called _gravatar to either 'shared' or 'layouts' ... i'm leaning towards shared...any thoughts?
 
I usually put partials in the view directory for the thing they are associated with. For example, let's say I have a "profile" page, but another controller needs to display a profile inline. I will create a _profile partial, in views/profile, and then have the other controller render it using "profile/profile".
For partials that aren't associated with anything, then "shared" is good.
layouts should be for... layouts.
 
...I have gravatars in 7 controller's views right now
 
Standard disclaimer: I'm no Rails programmer.
 
:)
 
4:47 PM
should i let rails pages error & rescue completely so there utter failure or should i (without messing with errors/rescue) include if statements that just don't display the broken part of a page layout?
 
Do error handling at the highest level you can stand.
If you can stand to just let the exception happen, do that. If you can't stand that, but can stand to rescue it at the highest level and, for example, display a custom "oops" page, do that.
 
I was thinking of having each layout deliver the same oops msg leaving rest of app functional
(in this case gravatar wouldn't crash the whole dang thing - I find that rediculous)
 
5:30 PM
How can I map [[[:a],[:b]],[[:c],[:d]]] to [[:a],[:b],[:c],[:d]]. I think that what I want is array.map {|e| *e}, but I get a syntax error.
 
a.flatten.map { |e| [e] }
 
But that doesn't work for multi element arrays. I know the arrays will be 2 elements long. For example: [[[:a, :a1],[:b, :b1]],[[:c, :c1],[:d, :d1]]] to [[:a, :a1],[:b, :b1],[:c, :c1],[:d, :d1]]
Also, why doesn't splatting work?
 
Hmm... Foo has many Bars... f = Foo.new(bars: [<many bars>]); f.bars => [] but why
 
Context?
 
What is <many bars>?
 
5:37 PM
Are you sure it won't work - I might be misunderstanding... apidock.com/ruby/Array/flatten they got some deep arrays in these examples
 
@thesecretmaster Oh, now you give us the entire problem :) a.flatten(1)
 
Oh, the argument to flatten is the depth?
 
As I understand, the arg is how many levels down to flatten
(think recursive use, 3 will flatten it 3 times)
a = [[[bacon], cheese], burger]
a.flatten(1) => [[bacon], cheese, burger]
a.flatten(2) => [bacon, cheese, burger]
(say it with the commas, so you can reveal in the glory that is "a")
 
user5870134
Hello, guys!
 
user5870134
@Mirv run the "flatten" function 1,000,000 times to see what happens :)
 
user5870134
5:44 PM
Although I do wonder, would the function break if the list couldn't be flattened any more?
 
...Idk mango...I'll have to think on this....to .... 'digest', this if you will it...
 
user5870134
Questions that actually make you think are the best ones to ask.
 
user5870134
OK, that's it from me guys. I have to get back to work. Goodbye!
 
See ya later, Mango.
 
@WayneConrad Just an array of some Bar model object
 
5:49 PM
bye
 
@Jared Then you don't need the outer []. Change [<many bars>] to <many bars>.
 
I've got this working controller test suite....right? Well I broke it just copying and pasting over to the other controller to use....it's like the new broken test suite infected the older working one!?!
 
Sounds like some fun investigation ahead.
 
i'm doing it smart....I undid all commands to the working test suite (recipes) & made sure not to save any changes, looking at github repo for diffs & restarting the workspace first
 
Oh, be sure to kill... um... that evil caching test thingy that Rails projects use. I forget its name.
spring. Kill/stop/etc/ spring and see if it gets better.
Then uninstall it.
 
5:55 PM
spring
i uninstalled it
 
@WayneConrad Ah i meant it was new(bars: [Bar.first, Bar.last])
Then when I call .bars it returns an empty AR set. I need to do some validation on that has_many before I save but its always empty =\
 
Is it possible that [Bar.first, Bar.last] evaluates to [nil, nil]?
 
oh man...it is still active somehow!
spring is the devil....I removed from gem file and did bundle update/install repeatedly, then ran the gem uninstall later
 
nope when I do this...
f = Foo.new
f.bars << Bar.first
f.bars => [Bar.first]
i'm assuming its some weird 'gotcha' with AR and this record not actually existing since the foo_id isn't populated on the records yet
 
@Mirv I use "ps" to find the spring forks and then "kill" to kill them. They can be very persistent things.
 
6:02 PM
go to rails console & see what the Table.find(<whatever>) & ensure it's populating?
wayne...did like 3 times now
 
If "kill" isn't killing them, then "kill -9".
 
(something is calling it back)
no joy on kill -9 either
 
Interesting.
 
6:20 PM
I rebooted the server basically, I had made two or three other changes, I can also just drop the branch if I can't find it
or rewind I suppose though that's always dicey
 
6:34 PM
hrm, I actually think i added in problematic code back into the view which I had removed to get test suite working
Yep...dang, for about 3 runs the code ran fine through the tests & suddenly, it's all errors...
I had refactored slightly to move the position of the nil? check
 
6:49 PM
Ok, don't laugh too hard .... this is how I got around the nil errors & avoided having to mock the gravatar ....
<% if user?(recipe.user) %>
<%= render partial: 'shared/gravatar_section', locals: {user: recipe.user} %>
<% end %>
(I am going to be doing this everytime I get a template error for this project I'm helping this guy get going)
Follow up question .... would you guys consider moving those 3 lines in a helper? I'm typing recipe.user twice
 
7:09 PM
Yes, I would.
 
Unrelated question .... when you guys take information in .... do you create a method in the controller to downcase all the params or capitolize .... or do you do this in the model & if so....would you do this by using a before_commit or save? I've never done datashaping in the model
ty Wayne!
 
If a model has an attribute that needs to be, e.g., capitalized, I make the model validate that, and perhaps ensure it.
What I do is to imagine that I am working with a very bad programmer who will find every possible way to code a bug. If the attribute must be capitalized and I do it in the controller, my idiot co-worker will surely create a new controller method that does not capitalize the attribute.
My idiot co-worker is me.
 
right!
ok, so the test is a good call - i don't know that it would break anything, but looks sloppy - so when I make the validation, as an extra ease of use....how would i then change the data before it hits the db? before_commit on model?
 
8
Q: Rails: ensure capitalization in model?

AndrewIn a Rails model, if I have a field that is for proper names, what is the best practice way to ensure they are uniformly capitalized despite potentially lazy input from users? IE let's say the model is Contact and the field is Name. No matter what the user inputs I want the words to be capitaliz...

A couple of ways to do this are shown.
 
derp, thanks!
Reminds me of that "Here, let me google that for you!" comic
 
7:22 PM
Heh, no problem.
There's a difference between googling something yourself and when someone else googles it: That someone else kind of knows what they are looking for, and can reject search results that are stupid.
 
7:36 PM
Yea, its the difference between googling 'how to get to cities the fastest' versus 'travelling salesman problem'
 
yea :)
I live in awe of people who solve travelling sales man issues!
 
7:55 PM
The only difference between me and a guy off the street is I know what to google ;;
2
 
And that is why we get paid top dollar to google program
4
 
^.^
...always wanted to sit down at the interview where they ask how would you solve a problem you didn't know the answer too & say, "First of all, those don't exist & secondly, 'google'" .... then if they ask me to elaborate answer them in the genie voice, "That answer is faaaaAAAAaaaar beyond your power of comprehension"
 
One heuristic for the TSP involves hiring a great many salesmen and tracking their movements.
 
Ok, second part of the helper wrapping....I've got this gravatar_for helper (in application_helper)..., it goes on a partial with 20 lines of html, then I call a method in recipe_helper from the view that returns "Obj is nil" or render's the partial....is that not the definition of codesmell?
 
Maybe? It's hard to tell from the description.
 
8:02 PM
Well, I believe it to be codesmell, because I have to verify the user is not nil in at least 2 if not 3 places
the gravatar_for could be used by someone else later, ... so maybe i return nil back thru the chain?
instead of checking at each step?
 
8:13 PM
I would try to make a single partial that includes the gravatar. That partial would call a helper gravatar_available? that checked to see if the gravatar was available. Everything includes that partial; that partial always calls the helper.
 
8:59 PM
I've got two different sets of requirements about how they are arranged in the layout of the partial and what information is being shown (for use of gravatar_for). So two partials off the main display seems about right...alternatively, I could do a bunch of if statements (like 6) with the shared stuff on there too....this idea is less appealing to me - especially since it's in ERB (messy) & not slim or haml
 
@WayneConrad I just got someone else to hate magic
 
Hah! What happened?
 
besides me?
 
9:22 PM
I was doing sound stuff, and I was asked "how does that computer get to the board" and I answered "magic" to which they replied "I hate magic"
 
Or maybe they hate programmers who say "magic" when an explanation was wanted ;)
 
I hate magic, why should I trust a program to manage my buffers?
 
 
2 hours later…
user5870134
11:42 PM
Developer === Google-Fu Master
 

« first day (1259 days earlier)      last day (1996 days later) »