#RubyOnRails - 23 September 2017
« Back 1 day Forward 1 day »
[07:41:29] timdotrb: I’m getting an error trying to query a model via: Model.find_by_slug(slug).select(:updated_at), NoMethodError: private method `select’. I am using both mysql and mongoid.. I’ve set my orm to active_record in my application.rb but I think mongoid is somehow messing with my queries?
[09:56:20] xco: hi. i my rails app i’m having “no implicit conversion of nil into String” on @start = Time.parse(params[:start])
[10:09:34] tbuehlmann: you could check for a present value using if, but maybe the controller isn't the right place for it
[10:10:58] xco: that variable has nothing to do with a model, where would be the place for it then if not in the controller?
[10:12:19] tbuehlmann: maybe you're missing a model, or a form object, or something like this. maybe I still don't know what that value is doing at all
[13:44:39] patientplatypus: any help would be very much appreciated - i am completely new to rails so its pretty easy
[13:44:58] tbuehlmann: patientplatypus: hey! what you already have in place is a concept named "validations" and people tend to place them in the model
[13:45:34] patientplatypus: ok - can you tell me how to do the model way and the way that im doing it currently?
[13:45:57] tbuehlmann: sure. when you try to save a user and the validations fail, the user is not saved to the database and user.errors includes the errors so you can display them in the view
[13:46:30] tbuehlmann: let's start with the email. if it's supposed to be unique across users, try this in your User class: `validates :email, uniqueness: true`
[13:48:47] tbuehlmann: here's a nice guide: http://guides.rubyonrails.org/active_record_validations.html
[13:49:31] patientplatypus: ok that seems right...cool. So two more questions - 1) how do i do this by searching the User model and if statements, and 2) how do you print variables to console?
[13:52:11] patientplatypus: sure - in the hastebin i linked to I want to take the the params that I passed to the server, search the available records in User model, and only add a new record based on whether the variable name is unique. The wrong way to do it for sure, but knowing this pattern may be useful elsewhere. It is linked as an example in the hastebin above
[13:55:18] tbuehlmann: you would do something like this: https://gist.github.com/tbuehlmann/07baa8d82bff4994c20193411ab5150a
[13:56:00] tbuehlmann: you can also try puts params first, maybe params[:name] is just not set and you just don't see it
[13:57:35] patientplatypus: tbuehlmann - that may not be quite what I'm getting at. this tells me if the user is saved or not. definitely useful, but i would like to know how to search the collection of users for unique passwords from the controller and output whether the password is unique or not. is this possible?
[13:58:49] patientplatypus: well...i would like to know how to search a model from the params i pass in the controller. That i think is useful
[13:59:26] tbuehlmann: searching models in general works like User.where(name: 'some name'), whicht returns a relation
[14:00:05] patientplatypus: ah? matthewd why not? should i be using User.where(name: 'some name') as suggested instead?
[14:00:58] matthewd: But `find` takes an ID; it can't tell that you're passing a name/email/whatever
[14:04:21] matthewd: Note that `where` returns a relation (a set of records), not just a single record -- so it might return an empty list, but never nil
[14:05:50] patientplatypus: having trouble reading the output correctly. thanks guys i think i got it for the moment
[14:57:01] patientplatypus: ok i have another question - i am trying to follow along here (http://guides.rubyonrails.org/association_basics.html) for a basic one to many association.
[14:57:34] patientplatypus: i have the following: @post = @user.posts.create(post_params) in my create section of my post controller. Unfortunately I am getting the following error:
[14:58:33] patientplatypus: so the compiler does not understand what I mean by @user.posts - it doesn't see posts even though i am following the example. Does anyone see what I am doing wrong?
[15:04:38] patientplatypus: @tbuehlmann i would set it by a curl command to set a user and get a token? this is part of where i am confused on how a one to many relationship should work - how does it know what user is currently 'active' so it can put the post in that users bucket. in the examples ive seen online there does not appear to be a way to set the user you want to associate with that post in the controller.
[15:07:15] patientplatypus: wait, can i specify that the user_id is a unique email like we were talking about earlier?
[15:10:14] patientplatypus: but say that I DO have this column....im still unsure how to set the column to the right user
[15:11:56] tbuehlmann: I don't know your usecase. so, shall every user (of the API) be able to create posts for others users? or are users only supposed to create posts for themselves?
[15:14:00] patientplatypus: well it will be a blog, in this case it will be that a user can only post for themselves, but each post will have a comments model (to be made) that will allow every user to write to every post
[15:15:21] tbuehlmann: okay, then you need some kind of authentication for your users. so when one of them tries to create a post, the user needs to be authenticated and the post will automatically tied to the creating user
[15:19:12] tbuehlmann: great, then what you need is a before_action method that is called before the actual create method from your posts controller. it would find the user to the corresponding token and set an @user variable
[15:28:07] tbuehlmann: start with something like this: https://gist.github.com/tbuehlmann/e1e82dbebfe8a50b00b193373d9e13a6
[15:49:17] patientplatypus: https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[15:49:46] patientplatypus: do you see what I'm doing wrong here? im getting undefined local variable 'user', but i think im following your pattern
[15:50:04] patientplatypus: NameError (undefined local variable or method `user' for #<PostsController:0x007fc4be6de310>):
[16:08:17] patientplatypus: ok so what's the difference between @user and user? That solved that problem, but now i have another
[16:11:22] patientplatypus: https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[16:12:37] patientplatypus: compare this with https://gist.github.com/tbuehlmann/e1e82dbebfe8a50b00b193373d9e13a6 that tbeulhmann sent and it is very similar with the .posts method
[16:15:35] patientplatypus: https://stackoverflow.com/questions/31296944/undefined-method-posts-for-nilnilclass says that this is becasuse @user is nil and therefore .posts is being called on an undefined value
[16:40:01] patientplatypus: the issue is that while that might work it doesn't differentiate between the token authentication and the find method - so if it ever breaks i dont know if its the tokens or the find that is breaking. Also, i want to try and keep them separated in development so that i dont have to keep logging in while i post
[16:40:31] patientplatypus: also also, i think i understand your way but my way should work and the fact that it continues not to is incredibly confusing to me
[16:40:50] patientplatypus: https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[16:41:48] patientplatypus: it is like less than half a dozen lines of code - this should be something easy to solve!
[16:45:55] patientplatypus: updated public gist https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[16:48:44] patientplatypus: ok...but why am i getting the error that I'm getting - I don't understand
[16:51:27] patientplatypus: i need to search all users and assign to @user before i can assign to @post no?
[16:52:44] tbuehlmann: and the syntax error comes from `User.find_by :email params[:user_email]`, which should've been `User.find_by email: params[:user_email]`
[16:54:26] patientplatypus: because i want to be able to search the User database - there will be times where I want to find a user as supplied by email that is NOT the current user
[16:55:05] patientplatypus: also because i should be able to search the fricking user database....i mean this should be a thing i can do!
[16:57:39] patientplatypus: the problem is that i cant specify a user email in a post request, and search the User database for the user that is tied to that email, and then make a post that is tied to that user
[16:58:31] havenwood: patientplatypus: User.find_by(email: 'email@example.com').posts.create(post_stuff: 'yerp')
[16:59:15] patientplatypus: havenwood is there a reason that my before_action and methods are not working?
[16:59:43] havenwood: I'll get some coffee and look. Seems tbuehlmann said above there was a syntax error. Did you fix that? Have you gisted latest code?
[17:01:44] patientplatypus: havenwood here is the latest gist https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[17:11:15] patientplatypus: havenwood it appears that your solution does not work .... updated the repo with the comments (see the posts controller section) https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[17:18:59] havenwood: patientplatypus: colon on the wrong side of `email`, look up Ruby 1.9 Hash syntax for Symbols
[17:19:43] patientplatypus: and the syntax error comes from `User.find_by :email params[:user_email]`, which should've been `User.find_by email: params[:user_email]`
[17:19:44] patientplatypus: From https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580 on line 57 of my post_controller I have
[17:19:48] patientplatypus: If there is something wrong havenwood, please tell me I followed what tbuehlmann suggested however!
[17:22:53] patientplatypus: ok now it should be up to date https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[17:25:04] patientplatypus: "exception": "#<NoMethodError: undefined method `before_action' for #<Class:0x007ff38739fe18>\nDid you mean? before_commit>"
[17:27:38] havenwood: patientplatypus: I just scanned your gist, but trying to use controller methods in your model?
[17:28:17] havenwood: Learning Ruby syntax would help easily spot the syntax errors. Learning Rails would inform what's going on with the before_* stuff.
[17:29:11] havenwood: patientplatypus: Show the before_* in the Active Record Callbacks docs that you're trying to use.
[17:29:41] havenwood: patientplatypus: No, what I'm saying is that you're using a method for controllers in your model.
[17:29:59] havenwood: patientplatypus: The reason I point you to the Model docs is that method is nowhere to be found.
[17:31:13] patientplatypus: i meant to say that i do not see where it is the case that i use a method for controllers in my model. Can you tell me specifically where you see that i am doing that?
[17:31:39] patientplatypus: second, before_action is, to my understanding, simply a lifecycle hook. why it is not working is very confusing to me
[17:33:08] havenwood: patientplatypus: https://gist.githubusercontent.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580/raw/d925aaa3561e010dae210a6df910f0c8b2216fef/postmodel.rb
[17:45:33] patientplatypus: https://gist.github.com/patientplatypus/dc64c5d6d4d0657dea50ad35bc92c580
[21:03:32] timdotrb: I’m getting a large amount of data from an API and storing it in Mongo. I have a couple instances where the data I receive is too large for Mongo to store in a single document.. Was wondering if anyone had any suggestions on how to solve that issue?
[21:03:50] timdotrb: Would a different nosql db solution be better (something with a document size >16MB?)
[21:10:07] patientplatypus: https://gist.github.com/patientplatypus/af24c425da672a9fd09b783e9e52a103
[21:20:42] patientplatypus: https://gist.github.com/patientplatypus/af24c425da672a9fd09b783e9e52a103