|
I have an EXTREMELY rudimentary question. I've set up two models: Games and Consoles Console has many games Game belongs to Console I'm creating New games and assigning consoles to them. How is it that I can say: game.console.title and get back the title of the console assigned to the game. However, I can't say "console.game" and get back all the games that belong to that particular console. The Games table is getting the console_id, but the Console table isn't filling up with multiple game_ids as they're being assigned to consoles. What fundamental ActiveRecord concept am I missing here? Should I be inserting game_ids into the console table at the same time that the console_id is being inserted into the game table? How do I ultimately list all games for a particular console if the console model isn't storing game_id? EDIT: This is just an exercise as I'm wrapping my head around rails. I won't be putting anything of this nature onto the web. Here's a pic of my ugly interface so you can see what my New Game action looks like plasticbugs fucked around with this message at 10:19 on Oct 11, 2009 |
# ¿ Oct 11, 2009 10:17 |
|
|
# ¿ Apr 27, 2024 00:24 |
|
Sewer Adventure posted:console.games will return an array of games Nope, I'm still getting back an empty array for each console. Here's my ruby code for assigning a console to a game using a drop-down list: code:
code:
I thought ActiveRecord provided a way to go through @gameconsole to pull each console's games. Is that an incorrrect assumption? I think I'm missing an important concept here. I assumed that ActiveRecord automatically handled this without a join table. Aren't join tables mainly for HABTM relationships?
|
# ¿ Oct 11, 2009 22:08 |
|
NotShadowStar posted:Nope, no need, that's 'has many and belongs to'. AR is smart enough to do backwards association without having to have foreign keys in both directions. When you say has_many :games, and then you say That helps a lot. Thanks. I think my bigger issue was with using my app's "script/console" to update each record and it wasn't showing the state changes on my other model's instances. When I actually got to coding my views, the pages displayed as they should with each console spitting out their associated games and each game knowing which gaming console it belonged to. Thanks again, I'm saving that chart. It will be very handy.
|
# ¿ Oct 11, 2009 23:32 |
|
Molten Llama posted:If you're not explicitly calling save after making a change, there has been no change as far as the app's concerned. It's pulling from the DB, and without a save, the DB is untouched. You're right. I wasn't calling save on the right instance. Seems to be working as advertised, now. Thanks.
|
# ¿ Oct 12, 2009 01:10 |
|
NotShadowStar posted:Another thing, anything and everything, whenever it writes back to the database, no matter if it's automatically saved like .create or explicitly like .save always always always goes through validations unless you really tell it to save_without_validation. Which reminds me, your Game class really should look like: This is great advice, too. I'm seeing the importance of validations and well-written error messages as I get deeper. Pardot posted:The other non-obvious, big thing you need to get in the habit of is to use add_index in your migrations on at least your foreign keys. Documentation. I'll start making it a point to add indexes on all my foreign keys. I'd like to think I'm working towards "best practices", so thanks for the advice.
|
# ¿ Oct 12, 2009 05:28 |
|
Can someone please help me understand why my code is so unwieldy? I've been teaching myself, but it looks like I'm missing some remedial Ruby concepts. I'm writing a browser based game/following a tutorial as another learning exercise and have a simple combat system set up. My fight(monster) method returns an Array called @combat. I'm trying to customize my model so the browser receives different messages depending on some of the equations inside the fight(monster) method. It works, but what I've done can't possibly be the best way to go about spitting out messages from a method that returns an array that 'should' be able to store a bunch of additional strings for me. Here's some of the code in my Player model along with questions (as comments): code:
plasticbugs fucked around with this message at 11:03 on Dec 2, 2009 |
# ¿ Dec 2, 2009 10:45 |
|
Pardot posted:I'm not happy with the perform_turn, but here's a shot. This is an enormous help. Thank you for your time. I'm taking your advice and will be looking into Rspec and Cucumber this weekend. Is Cucumber the best way to go about BDD? I know enough Ruby/Rails to be exceedingly dangerous, so is it too early for me to start learning how to write tests? Or is writing tests an integral part of learning (not just Rails but intelligent programming)? EDIT: typo plasticbugs fucked around with this message at 21:39 on Dec 2, 2009 |
# ¿ Dec 2, 2009 20:35 |
|
Operation Atlas posted:It is never too early to start writing tests. Learning rails without testing is kind of a guessing game and what works seems to be magic or an incomprehensible collection of mystery settings. Using rspec (or another testing framework) will take the guesswork out and will let you know exactly what broke when so that you can fix things more quickly, more easily, and with less magic. Thanks. One more question. Any advice on first steps? I see that Pragprog has an upcoming book on the subject. Until it comes out, is there another book you recommend?
|
# ¿ Dec 2, 2009 21:43 |
|
phazer posted:Assuming you mean The rSpec Book the beta PDF is a prefectly fine start. I went from 0 knowledge of cucumber/rspec to regularly doing BDD & testing in every project. Yep, I bought it last week and I'm amazed at how much I've been able to grasp so far. The PDF will live on my Sony Reader until the paper version comes out. I have a question about IDEs and design. Is there a way I can set-up Textmate to show a live preview of my edits in a browser window - like if I'm tweaking CSS? If not, is this something that Coda does with a Rails project?
|
# ¿ Dec 9, 2009 00:45 |
|
I stupidly upgraded my Ruby install on OS X Snow Leopard to 1.9.1 while following a new Rails book tutorial. To my credit, I followed the directions word-for-word, thinking what a good idea it would be to have the latest version of Rails AND the latest stable version of Ruby, too. Now WEBrick crashes like a champ (EDIT: mongrel fails to build, too!) - obviously Rails isn't ready for Ruby 1.9.1. Is there an easy way to downgrade my Ruby back to 1.8.7? EDIT 3: Fixed my code and WEBrick is still crashing. My Ruby is in: /usr/local/bin/ruby Also, if you are gracious enough to help me, please assume that I'm a complete idiot. EDIT 4: Holy poo poo. After an hour of wrestling with installing and uninstalling Ruby, Gems and Rails, my ultimate solution was to TRASH my Ruby binary, my Rails binaries (rake, heroku et al.) AND my entire Gem folder (my 1.8 AND my 1.9 gems) - every single Gem. I reinstalled Ruby 1.8.7, reinstalled RubyGems and all my gems including Rails. It works. Lesson learned. Don't do what I did. plasticbugs fucked around with this message at 09:44 on Feb 3, 2010 |
# ¿ Feb 3, 2010 06:39 |
|
NotShadowStar posted:Use rvm. I've come to love it like a gay go-to bottom that's always willing. It works even without a system Ruby. Halfway through my process I found rvm, but I was well past the point of no return. I didn't realize the amount of things that were about to break as I "sudo rm'd" Rails binaries one-by-one from /usr/local. I will begin using rvm to avoid this nonsense with future updates.
|
# ¿ Feb 3, 2010 19:19 |
|
LordNova posted:Anyone else going to the LA Ruby conference this weekend? drat, I wish I saw this sooner - spent a day by myself not talking to anyone. I went for the class day on Friday, but not for the Saturday talks. Did you end up going? I took the Nokogiri/Mechanize class and the Rubygames/Gamebox class. Both were well worth the extremely cheap $60 per course fee. The guy who created Nokogiri gave a nice primer on Xpath and CSS selectors which was a great foundation to learn from.
|
# ¿ Feb 26, 2010 07:48 |
|
I've got another very rudimentary Ruby question. I'm attempting to access data inside an XML file with Nokogiri. The data is coming back as a Hash with a nested Array. Then, inside that Array is a Hash with another Array nested inside it. Inside THAT Array is a Hash with the data I want to access. Given: code:
|
# ¿ Mar 10, 2010 19:26 |
|
I should clarify, I wasn't using Nokogiri. I was using a gem for a specific API, which I'm guessing has its quirks.Flobbster posted:Does this work? Yes, sorry. a, b, and c are strings. EDIT: That's the answer I was looking for. Thanks! EDIT: Deleted stupid question. Learning all this on my own with nothing but some books and Google is challenging, but this thread has been such a huge help. Thanks. plasticbugs fucked around with this message at 09:15 on Mar 12, 2010 |
# ¿ Mar 10, 2010 20:13 |
|
I'm confused again. Another dumb question: I'm working with a Nokogiri object and am attempting to populate a page with TV episode titles and links to those titles with data from an XML file. This gets all the links: code:
code:
code:
This was my approach to putting the results of both operations into the array above, and it worked, but it seems VERY sloppy: code:
2. Is there a better way to put those results into a structure that I can iterate over in my view to create a list of Episode titles and links? I feel like I'm missing the obvious, easy solution to displaying this data in my view. plasticbugs fucked around with this message at 09:28 on Mar 12, 2010 |
# ¿ Mar 12, 2010 09:12 |
|
I'm checking in to say that I've solved my own problem from a few posts up with regards to creating a hash in one step from an XML file using Nokogiri. For anyone getting started with Ruby or Nokogiri, this may be of some help. Here's my refactored code: code:
|
# ¿ Mar 14, 2010 22:03 |
|
Pardot posted:You should use Enumerable#map Thanks for this! I'm reading up on it right now.
|
# ¿ Mar 15, 2010 05:34 |
|
EDIT: I'm an idiot. I had a column in my model named "Type". plasticbugs fucked around with this message at 04:15 on Aug 23, 2010 |
# ¿ Aug 23, 2010 04:03 |
|
Nolgthorn posted:I've done this or similar a lot with Rails. I'm going to start using your thesaurus trick. This wasn't my first run-in with a 'protected' word.
|
# ¿ Aug 30, 2010 20:59 |
|
I have another very rudimentary question. I recently reworked my app in progress which until recently had three separate models called "Game", "Movie", and "Product" - but they all basically had the same controller structure and had nearly identical views and model definitions. To prevent this repetition, I have created a model called "Thing" and I subclassed it to create "Game", "Movie", and "Product" models. So my model definition for "Game" looks like this: code:
Right now if I do: code:
EDIT: Would I be better off going back to my 3 separate model solution, each having their own table? Meaning, I would not have a very DRY app, but at least I could rely on Rails conventions without much extra hassle. plasticbugs fucked around with this message at 20:36 on Oct 20, 2010 |
# ¿ Oct 20, 2010 20:25 |
|
NotShadowStar posted:Yes. Don't do what you did. It's clever, but unnecessary. Sometimes DRY goes too far. Also, subclassing doesn't do what you think it does. It looks like a little repetition will likely save me from myself and the quirks of STI. Thanks for your help! I'll take a look at how I've structured my database to ensure I'm not overly complicating it. Having "Games", "Movies" and "Products" in separate tables seems like it may speed up queries, and will at the very least simplify my queries.
|
# ¿ Oct 20, 2010 23:53 |
|
I need a login system for my simple CMS site. Instead of rolling my own, I'd like to use an existing library/gem. Between Authlogic, Devise and Omniauth, which do you guys recommend for an absolute beginner? I was leaning towards Devise. I wanted to avoid rolling my own simply because I think I'd actually learn more by working with modules and figuring out how to integrate them into an existing project (something I haven't quite done yet). Edit: Holy poo poo. Devise is dumb simple to use and configure. I think I have my answer. plasticbugs fucked around with this message at 09:32 on Oct 22, 2010 |
# ¿ Oct 22, 2010 07:45 |
|
Please save me from myself. I'm interacting with the Amazon Product Advertising API. One of the item attributes kicks out a verbose, formatted product description. However, it prints to the browser as a bunch of escaped html entities, like so: ________________ <i>New Super Mario Bros. Wii.</i> Supporting 2-4 players in side-scrolling co-op and competitive platforming action, and featuring a mix of fan favorites and new characters, new powerups and various input options via the Wii Remote, it is destined to become an instant classic in one of the most beloved game franchises of all-time. <style type="text/css"> .caption { font-family: Verdana, Helvetica neue, Arial, serif; font-size: 10px; font-weight: bold; font-style: italic; } ul.indent { list-style: inside disc; text-indent: -15px; } ________________ I'm sure there's a built in Rails method that cleans this up, and prints to the browser as parsed HTML. I just don't know what it is. EDIT: I figured it out: require 'cgi' CGI.unescapeHTML() EDIT EDIT: For anyone that cares: Once you've unescaped the HTML entities, you still need to format your string with the html_safe method. Then, Rails will actually print the formatted HTML to the view. plasticbugs fucked around with this message at 06:00 on Nov 2, 2010 |
# ¿ Nov 2, 2010 05:30 |
|
I'm writing a simple app that allows you to input a text message and you get back a random code number so you can retrieve the text message later. I have a Message model with two columns: contents codeNumber I would like the app to generate a unique codeNumber and save it to the database along with the message contents. If I go into the console and do code:
If I do: code:
What am I doing wrong? My Message model: __________ class Message < ActiveRecord::Base def codeNumber generateCode end private def generateCode Code Logic in here end end ____________ EDIT: I may have found a solution, but it may not be elegant. Please look below. I revised my Model to institute a callback. This still seems wrong, but it works. code:
plasticbugs fucked around with this message at 08:14 on Mar 30, 2011 |
# ¿ Mar 30, 2011 07:51 |
|
Pardot posted:Yeah, you're on the right track, but this is probably what closer to what you wan Thanks for your help! I'm reading up on memoization right now.
|
# ¿ Mar 30, 2011 08:56 |
|
I have some routing weirdness going on with my 2-page site. I'm not sure if it's my routes or my controller that is causing the problem. Here is my controller action: code:
code:
http://mysite.com/messages. And, that doesn't even go anywhere. What's causing that address to pop in there? I'm assuming it's REST, but I don't know how to avoid that and keep the benefits that REST is providing (simplicity). If validation fails, is there a way to say render the new action as the root URL? If I use 'redirect_to :root' instead of 'render', I'm not able pull the validation errors into the page.
|
# ¿ Apr 4, 2011 01:48 |
|
NotShadowStar posted:
I'll try this right now and will report back. Thanks! EDIT: I ended up leaving the route in there with ":only" but went on to add a route to match the RESTful aberrant route generated by my call to 'render' after a validation fails: match '/messages' => 'messages#new' I don't mind this workaround and it keeps my app from breaking. Here's the site by the way: http://www.3dstxt.com It's basically a messaging site crossed with bit.ly. It's made for users of the Nintendo 3DS, which only supports 16 character messaging. It's one of my first Rails projects. Thanks for your help! plasticbugs fucked around with this message at 08:53 on Apr 4, 2011 |
# ¿ Apr 4, 2011 02:09 |
|
The first Rails site that I've ever put into production is now live at 3dstxt.com. However, my question may not be Rails specific. Is there an easy Rails Way to prevent Google from indexing certain pages? I'd rather Google didn't crawl my app's "show" pages, where users are posting semi-personal content (email addresses, 3DS Friend Codes, etc). EDIT for clarity: Because of how the URLs are created, this includes any URL that matches 3dstxt.com/XXXXX I still would like Google to index the root at 3dstxt.com/ and possibly blog pages or help pages that may eventually exist. Is robots.txt the only option? plasticbugs fucked around with this message at 20:47 on Apr 6, 2011 |
# ¿ Apr 6, 2011 20:38 |
|
NotShadowStar posted:robots.txt in the public directory would be your best bet. Easy, simple, and all the search engines worth a drat respect the directives in it. I uploaded a robots.txt just now. Thanks for the help! I started seeing some people sharing their email addresses and Nintendo Friend Codes, and I didn't want Google indexing that stuff. It'd be nice to have some extra free search traffic, but not at the expense of someone's privacy. Hopefully Google will still index my site's root. As for Sinatra, I considered it - I had built a simple Twitter app using Sinatra and an online tutorial. However, I'm still wrapping my head around VERY basic concepts and tools - git, sessions, routes - so I stuck with Rails.
|
# ¿ Apr 7, 2011 06:01 |
|
Cock Democracy posted:If you don't want google to index these pages, consider whether you really want unauthenticated users to see these pages at all. These pages are intended to be viewed by total strangers (other Nintendo 3DS owners) who receive a URL that's being broadcasted from the user's Nintendo 3DS. So I can't restrict pages based on authentication.
|
# ¿ Apr 8, 2011 20:33 |
|
My validations are causing some strange goings on with my view. The site basically lets the user create a message and gives the user a randomly generated message_url for sharing that message - like bitly. Users with accounts can create messages and choose a custom message_url like example.com/myurl. If a user creates a message - but chooses a custom URL that is already taken, the custom url fails validation. The way I've built my callbacks, the system spits out a randomly generated url if it finds the chosen custom url is already being used. In a nutshell: 1. if message_url is blank or nil, a randomly generated url is created and the record is saved as a valid message. 2. If message_url is not blank and passes validation, the record is saved as a valid message with a custom url 3. If message_url is not blank and fails validation, the record is NOT saved and the view is re-rendered with a randomly generated message_url. The problem is, after the validation fails, the current view renders with a suggested url, but it also presents the very message which failed validation at the bottom of the list of the user's messages. The record is not valid and the its link goes nowhere. Why is this happening? Thanks in advance to the generous Rails users in this thread who continue to offer priceless help and advice. Here is a picture: This is my User controller's show action: code:
code:
code:
|
# ¿ Apr 17, 2011 23:17 |
|
Anveo posted:Not sure if the 'random-ness' is a feature but if that doesn't matter just do a base conversion on the primary key: UPDATE: Regarding my problem above. I took the cowards way out. In order to prevent the invalid record from showing up in my view, I'm checking to see if there are errors on any of the messages. If there are, the view won't render them. Problem solved, but still not happy that I couldn't find a more elegant solution. Thanks again for the help Anveo and Obsurveyor. Thanks for the tips. I did want to obfuscate the Message ID somehow and I probably will soon to avoid the expensive lookup by message_url operation. Also, I believe I left out some crucial info that may point to why my view is showing an invalid object. The Create action from my Messages controller, which duplicates a BUNCH of stuff in the Users Controller Show action (from my previous post), but if I remove any of the lines in either controller, Rails complains of nil objects. code:
plasticbugs fucked around with this message at 09:21 on Apr 18, 2011 |
# ¿ Apr 18, 2011 01:17 |
|
BonzoESC posted:If it's indexed in the database it shouldn't be expensive. Thanks for this. I'll look into it. One more question popped up. I want to limit users to creating 5 total items. Items belong to Users. Can I run a validation inside my Item model that won't allow a user to create a sixth item by checking self.user.items.count < 6 In pseudo-ish code (obviously, this doesn't work) class Item validates_numericality_of self.user.items.count, :less_than => 6 end
|
# ¿ Apr 21, 2011 01:07 |
|
NotShadowStar posted:I don't think a whole inner class would be necessary for just one validation. You could do it with I did exactly this. Thanks to both of you.
|
# ¿ Apr 21, 2011 05:03 |
|
I made the bad decision to begin my Rails app with Disqus comments. I figured it would be easy to export them when I outgrew the simple stop-gap solution it provided. I know how it's organized, but I'm having trouble figuring out a simple way of parsing all the entries. The XML file is basically set up like this: pre:<thread dsq:id="12345"> <id>foo</id> </thread> then, much further down the same XML file pre:<post id="177002220"> <id/> <message><![CDATA[This is my awesome comment!]]></message> <createdAt>2011-04-02T19:02:04Z</createdAt> <author> <email>me@example.com</email> <name>myname</name> <isAnonymous/> </author> <ipAddress>192.168.1.1</ipAddress> <thread dsq:id="12345"/> </post> This is what I have, but it just hangs. Am I even close? pre:require 'nokogiri' @doc = Nokogiri::XML(File.open("~/Downloads/disqus.xml")) @doc.css("thread").each do |t| @t = t @thread_id = t.attributes["id"].value @doc.css("post").each do |p| @p = p @post_id = p.attributes["id"].value end if @thread_id == @post_id c = Comment.new c.code = @t.css("id").text c.author = @p.css("author name").text c.contents = @p.css("message").text c.save! end end plasticbugs fucked around with this message at 06:08 on Feb 12, 2012 |
# ¿ Feb 12, 2012 06:04 |
|
UxP posted:There isn't much of a reason to separate out the threads and comments loops, just go for it all in one shot. I've never used css selectors for XML node traversal, so I'd honestly just go for basic XPath. Thanks so much for your help! This worked. I get in over my head quickly and this is an instance where I should go back to basics for a little while and learn more Ruby and less Rails. I also need to look into xpath, as it seems to be more powerful than using just CSS to traverse the hierarchy. EDIT: Can you or anyone recommend a good book or site to learn more about working with XML with Ruby or any other non C language? Originally I had it scoped with locals, but when that didn't work, I tried it the other way with all instance variables thinking maybe the first block wasn't passing variables into the next block. Me code pretty one day. plasticbugs fucked around with this message at 20:23 on Feb 12, 2012 |
# ¿ Feb 12, 2012 20:14 |
|
EDIT: I figured it out. My solution at the bottom - I didn't know about zip My site allows users to add video games to a collection and makes use of the Amazon API. The Games model has a string column called 'asin' (Amazon Product ID). When the User views their Games page, the Games controller creates an Array of all the @user.games ASINs and makes an Amazon API request using that array of ASINs. To work with the response, I convert it to a Hash and pull the data I need like this: @collection = response['Items']['Item'] So, if the user has 3 games, then @collection is an Array of 3 Hashes that I can iterate through (these hashes have things like Image URLs, Product Descriptions, Prices etc). I'd like to give the user the ability to delete a game on the page that lists all their games. My problem is this: I have @user.games (an array of 3 games) I also have @collection (an array of 3 amazon responses, that has no hooks back to my Games column) How can I do something like what's below in my view? @collection[0]['AwesomeAPIData'] | link to destroy item based on @user.games[0] @collection[1]['AwesomeAPIData'] | link to destroy item based on @user.games[1] @collection[2]['AwesomeAPIData'] | link to destroy item based on @user.games[2] I have a solution in place that does what I want, where I'm using the Amazon Response to find the ASIN and then doing a reverse lookup with @user.games.find_by_asin(asin), but it's REALLY kludgy. I'd rather pass in an array of game objects from my controller and link them to the Amazon response somehow. EDIT: More succinctly (with fake code), how can I: @everything = [@amazon_hashes, @game_arrray] @everything.each do | amazon, user_game_data| amazon.foo | user_game_data.bar end SOLUTION: @everything = @amazon_hashes.zip(@game_array) @everything.each do | x, y| x.foo y.bar end plasticbugs fucked around with this message at 00:31 on Mar 18, 2012 |
# ¿ Mar 17, 2012 23:33 |
|
My site lets users create a bunch of messages. The users#show page lists all of the user's messages BUT also includes a form at the bottom for creating a new message. So, my User Show action looks like this: code:
Is there a better way to avoid this problem than doing something like what I typed below in my view? This feels hacky: @messages.each do |message| if message.valid? <%= message %> end end
|
# ¿ Mar 21, 2012 04:26 |
|
prom candy posted:What does your create action look like? Are you creating the message in a separate messages controller? This pretty much all has to do with when you're setting the value of @messages. Here's my create action in my Messages controller. It's a little messy because Messages can be created without an account by design, so I have to cover a few cases. The case described above is where the User is signed in, and the message created isn't valid. code:
|
# ¿ Mar 21, 2012 06:21 |
|
|
# ¿ Apr 27, 2024 00:24 |
|
prom candy posted:So we're talking about the case where "!@message.valid? && signed_in?" right? Originally, I had a redirect action in there, but then my flash[:error] wasn't persisting. Either way, with either redirect or render :template, the extra invalid message was displaying in the user's list of messages. I didn't do a copy paste. I retyped my code into my post, I forgot that line. Here's what my create action actually is: code:
|
# ¿ Mar 21, 2012 19:42 |