Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
prom candy
Dec 16, 2005

Only I may dance

KarmaticStylee posted:

What rails hosting providers would you recommend? I am looking for something under $30/month if possible.

http://www.hostingrails.com/ sounds good. Any experience with them?

Slicehost for sure. There are a lot of guides out there on how to get up and running with them, and really part of the Rails experience involves figuring out how to set it up. Luckily for you it's gotten a hell of a lot easier over the past few years. I don't miss configuring dispatch.fcgi one bit.

Adbot
ADBOT LOVES YOU

prom candy
Dec 16, 2005

Only I may dance

NotShadowStar posted:

Why is Mechanize needed for a Twitter app?

It probably actually logs into Twitter rather than using the API.

prom candy
Dec 16, 2005

Only I may dance
Just to add to that, you don't always have to inherit. I write classes all the time that don't inherit from ActiveRecord. Inheriting from ActiveRecord is when you have a one-to-one class-to-table mapping with your database. If you want to write a class that has other responsibilities, go hog wild.

If you want to use { and } rather than do and end you can, but the preferred Ruby style is to use do and end for multi-line blocks and { and } for single line blocks.

code:
  people.each do |person|
    person.name = "Hi"
    person.save
  end
vs.

code:
  people.each { |person| person.update_attributes(:name => "Hi") }
(These examples do the same thing).

Finally, not sure who asked it or if it's already been answered, but yes you can craft your own SQL if you need to do something complicated. However, ActiveRecord is powerful and expressive, you don't need to do that very often.

prom candy
Dec 16, 2005

Only I may dance
The .find method has the ability to return either one object or a collection of objects. Most of the time .find isn't used anymore and convenience methods are used instead, but here's an example. Again, the code below is no longer the correct way to do this:

code:
  # Return the first person in the database
  @person = Person.find(:first)

  # Return all the people in the database as a collection object
  @people = Person.find(:all)
The new and better way of writing the same thing is:

code:
  # Return the first person in the database
  @person = Person.first
  
  # Return all the people in the database
  @people = Person.all
Now, say you had @people, a collection of people, and you wanted to loop through it and print each person's first name. You would define a variable within block scope called "person" and access each person that way:

code:
  @people.each do |person|
    puts person.first_name
  end
In this case, person is defined in the block scope, but you can call that variable anything you want.

code:
  @people.each do |dude|
    puts dude.first_name
  end

prom candy
Dec 16, 2005

Only I may dance
It would probably be a good idea to check out something like http://railsforzombies.org/

prom candy
Dec 16, 2005

Only I may dance
It's split into Model/View/Controller. The framework itself takes care of including all the necessary files so they know how to talk to each other. Let's assume a simple scenario where you have a database table called "people" with columns "first_name" and "last_name". Your goal is to print out a list of these people, ordered by last name, when someone visits http://yoursite.com/people.

First the model (app/models/person.rb). This model doesn't need any code because it inherits from ActiveRecord::Base. That means that you automatically get methods like "all" and "find" and "save." However, if you have any heavy duty business logic you want to include it in the model. In our example I'll be defining a method called "full_name" which just concats the first_name and last_name methods. In my example I use "return" and I also refer to "self.", both of which are unnecessary and frowned upon, but they make it easier to understand in this example.

code:
# app/models/person.rb
class Person < ActiveRecord::Base
  def full_name
    return self.first_name + " " + self.last_name
  end
end
Next up is the controller. The controller is the go-between for your models and your views. This is where you query your model to prepare the variables that you'll use in your view. Any variable that starts with @ will be available in your view. The important thing to look at here is People.order(:last_name) which returns a collection.

code:
# app/controllers/people_controller.rb
class PeopleController < ApplicationController

  # When someone visits /people they will get this method
  def index
    @people = Person.order(:last_name)
  end
end
Finally, the view. This is what the framework will use as a template when someone visits /people

code:
<ul>
  <% @people.each do |person| %>
    <li><%= person.full_name %></li>
  <% end %>
</ul>

prom candy
Dec 16, 2005

Only I may dance

Physical posted:

I am at least familiar with MVC methodology and specifically have experienced using it with PHP and Code Ignitor.

The major difference between Rails and CI (in my limited experience with an older version of CI) is that Rails practices what's called "Convention over Configuration." Assuming you follow the Rails conventions of putting your files in the right places and naming them the right things (in relation to your database table names, for example) you don't have to explicitly include any files, it's all done for you. In CI if you want to reference a model from another controller you have to specifically say "i'm going to be working with this model" but you don't need to do that in Rails. You can pretty much access anything from anywhere if you know what it's called.

Look at the index action in the controller and the index.html template (I forgot to mention that the view would reside in app/views/people/index.html.erb). Rails automatically invokes app/views/people/index.html.erb for the action "index" in the controller "People" because that's the convention. If you wanted to you could render something else.

code:
  # app/controllers/people_controller.rb
class PeopleController < ApplicationController

  # When someone visits /people they will get this method
  def index
    @people = Person.order(:last_name)
    render :template => "dudes/list" # This will use app/views/dudes/list.html.erb instead 
  end
end
Similarly, your "Person" model automatically knows it's dealing with your database table called "people" because Rails automatically links an ActiveRecord model with the pluralized version of itself. Again, you're not bound to this, you could just do:

code:
# app/models/person.rb
class Person < ActiveRecord::Base
  set_table_name "dudes"
  
  def full_name
    return self.first_name + " " + self.last_name
  end
end
The idea and the real appeal of Rails (IMO) is that it automatically does all the tedious stuff for you, provided you stick to the conventions.

prom candy
Dec 16, 2005

Only I may dance
Check out that Rails for Zombies site I linked, it's supposed to be really good for beginners. Bump this thread if you have any other questions, I'll keep an eye on it.

prom candy
Dec 16, 2005

Only I may dance
I know, I mentioned that above my code block. I wrote the example that way because the Ruby style would be a little confusing for someone who is new to Ruby. Adding the self and return hopefully made it clearer in the example but I would never write real code like that.

prom candy
Dec 16, 2005

Only I may dance
It might be a good idea to use nested resources for that. Here's a screencast about it: http://railscasts.com/episodes/139-nested-resources

prom candy
Dec 16, 2005

Only I may dance
I think I read on Github or Stack Overflow that suppressing the asset log messages is an option that's coming soon.

prom candy
Dec 16, 2005

Only I may dance

A MIRACLE posted:

I'm ready to kill myself. Okay not really but my friend/frontend dev guy did a pull request today where he basically rewrote our entire landing page, started using compass / blueprint, and now I'm running into problems with the asset pipeline deploying to heroku. It's gotten to the point where I'm ready to scrap the whole thing and redo it in Sinatra. Also I'm a little hung over because I got day-drunk at lunch, but still. Can anyone point me to info on the stylesheet link tree? I think that's where the problem is, like it's not precompiling a few stylesheets because they aren't in the tree or something. Here's what my log looks like:

code:
 ActionView::Template::Error (screen.css isn't precompiled):
2012-02-23T01:54:24+00:00 app[web.1]:   Rendered pages/home.html.haml within layouts/application (3.4ms)
2012-02-23T01:54:24+00:00 app[web.1]:     6:     -# Compass and blueprint stylesheets
2012-02-23T01:54:24+00:00 app[web.1]:     8:     = stylesheet_link_tag 'print', :media => 'print'
2012-02-23T01:54:24+00:00 app[web.1]:   app/views/layouts/application.html.haml:7:in 
`_app_views_layouts_application_html_haml___2737759042931766110_26806720'
Any help is appreciated.

If you're including an asset directly in a template that's not called application.js.*, application.css.*, or is not an image Rails won't automatically precompile it in production. You either need to require screen.css from application.css, or you need to add it to your list of precompiles in production.rb

code:
 config.assets.precompile += ["screen.css", "print.css"]
Edit: The asset pipeline is full of gotchas, make sure you read the documentation on it or it's going to drive you nuts. I speak from experience.

prom candy
Dec 16, 2005

Only I may dance

Physical posted:

How the heck do you get a debugger to work in a Win 7 64-bit environment using eclipse. All of these directions are so vague.

You're going to have a hard time finding help for that environment I think. When I develop Rails in windows (which is rare) I use Cygwin for the server. Neither Windows nor any of the IDEs are particularly well-supported, Cygwin takes away a lot of the pain IIRC.

prom candy
Dec 16, 2005

Only I may dance
I'm not totally sure. I don't know a lot of developers that run Eclipse. It seems like most Rails devs run development server via the command line (using Webrick or Mongrel or whatever) and then use their text editor of choice to write code (TextMate seems to be the most popular judging by screencasts, but Vim is up there too). By using these pre-packaged but somewhat poorly adopted solutions you may be setting yourself up to run into stumbling block after stumbling block, is there a reason you're determined to use Eclipse?

Edit: Also, #rubyonrails on freenode is a good place to ask for help, there are a lot more people there than there are reading this thread.

prom candy
Dec 16, 2005

Only I may dance
I can see the allure of wanting ruby autocomplete but honestly, there just aren't that many methods to memorize. It's not like PHP where there's no consistency in function naming or argument order. If you want a debugger I highly recommend just running your actual server via command line and dropping the debugger in your code where you need it. I know Eclipse is really popular, I just don't think it's very popular among Rails devs.

prom candy
Dec 16, 2005

Only I may dance
Use an underscore.

prom candy
Dec 16, 2005

Only I may dance
You mentioned earlier that you had access to a *nix box or VM? Honestly I would do my development there, Rails under Windows is painful. I have no idea why it wouldn't be dating your migrations, I've never seen that happen before.

prom candy
Dec 16, 2005

Only I may dance
Same project under *nix, same problem?

prom candy
Dec 16, 2005

Only I may dance
I have never heard of this issue ever, is this a fresh project or something you cloned at your new job? Is this a work issued machine or your own?

prom candy
Dec 16, 2005

Only I may dance
Is this set anywhere:

code:
config.active_record.timestamped_migrations = false
If you fire up a new Rails app with the exact same gemset, database config, etc, do you get the timestamps?

prom candy
Dec 16, 2005

Only I may dance

Physical posted:

That is not showing up anywhere in any of the project files. What file is it normally under? I want to put it in there with = true

application.rb

Open up rails console and type

YourApp::Application.config.active_record.timestamped_migrations

to see what it's set to. Replace YourApp with whatever your app's module is called in config/application.rb

quote:

Mac users, what makes up your rails development environment?

rails server
rvm
TextMate
github
mysqlol

prom candy
Dec 16, 2005

Only I may dance
Do you guys use postgre in production as well or just for development?

prom candy
Dec 16, 2005

Only I may dance

Doc Hawkins posted:

When people say "use postgres in development," it's short for "use exactly the same environment in development as in production, by the way use postgres."

Do you mind giving me a quick rundown of why it's better than MySQL, or pointing me towards some info about it? MySQL has always just worked for me so I've never really given other solutions much thought.

prom candy
Dec 16, 2005

Only I may dance
I let the generator do it for me, especially because you've been able to write the whole thing from the command line for a few years now.

prom candy
Dec 16, 2005

Only I may dance
Wow I am loving Sublime Text 2. Thanks for the recommendation!

Edit: Seriously, how the gently caress is it this fast? If you're a TextMate user you have to give this a try.

prom candy fucked around with this message at 04:05 on Feb 29, 2012

prom candy
Dec 16, 2005

Only I may dance

Scaramouche posted:

Also that example is as real as the day is long.

Make sure you update the thread when you inevitably post the story on Clients From Hell.

prom candy
Dec 16, 2005

Only I may dance
Your association method can't be named something that's already a column in your database. If you want to have something that belongs_to an owner your column should be owner_id. You're basically getting a method collision right now because with active record any field in your database (with a few exceptions) becomes a method on your model, and when you declare an association that also creates methods on your model. So when you type Thingy.owner it doesn't know if you're referring to the association or the value in the DB field.

Your issue is completely unrelated to the inheritance.

prom candy
Dec 16, 2005

Only I may dance
A big part of making yourself marketable in web dev these days is understanding front end development. Make sure you're learning javascript concurrently with whatever back-end language/framework you decide on. And I mean really learning javascript, not pasting jQuery plugin examples into your onclick attributes.

prom candy
Dec 16, 2005

Only I may dance
Have you looked into Capistrano? Your set up is a little more complicated than mine but if there's someone out there who's solved your particular problem it's fairly likely that they've done it with Capistrano.

prom candy
Dec 16, 2005

Only I may dance
Shot in the dark, but try


gem 'acts_as_archive', :git => 'http://github.com/xxx/acts_as_archive.git', :require => 'acts_as_archive'

Alternately, go harass whoever pushed that change as they probably already figured out a fix.

prom candy fucked around with this message at 15:45 on Mar 7, 2012

prom candy
Dec 16, 2005

Only I may dance
That seems to be a theme in this thread for you.

Edit: Seriously, tell your work to set you up with a proper dev. environment. Alternately, tell your linux lubbin coworkers to put linux only gems into :group => :linux and then install your gems with

bundle install --without linux

prom candy
Dec 16, 2005

Only I may dance
He's specified that it should use the Gem found at that GitHub location, and that version of the gem is breaking something in your app. Run the rake task with --trace and you can figure out where it's blowing up.

Breaking your co-workers' poo poo with one of your commits is generally considered a faux pas, he should be helping you resolve this.

prom candy
Dec 16, 2005

Only I may dance
Checkout ActiveRecord's update_all method. You're trying to update a number of records, correct?

prom candy
Dec 16, 2005

Only I may dance

Large Hardon Collider posted:

Actually just one. The site lists local businesses, each with a number of aspects you can rate, formulated as questions. The questions themselves are stored in the database, and I want to correct a typo in one of them.

So you're looking to do the most basic update operation then. Open up your console and do something like.
code:
id = <id of the rating>
rating = Rating.find(id)
rating.field = value
rating.save
Alternately:

code:
Rating.find(<id of the rating>).update_attributes(:field => value)
I don't mean to be a dick, but this is really basic stuff that you should have learned from a beginner's tutorial. It's going to be a long, slow learning process if you don't do some up-front reading on the basics.

prom candy
Dec 16, 2005

Only I may dance
Migrations are for structure but you can put content operations in there as well. However, that's not really the best way to go.

I would put it in a rake task and then run it on your different environments, or just do it manually from the console on your environments. It depends on if you're going to need to reuse the task or not. Will somebody cloning the project for the first time have to deal with this, or is it a case of somebody entering bad data into a production DB?

prom candy
Dec 16, 2005

Only I may dance
Just to clarify, what you've got there is a Hash inside of an Array.

@blah.data.class would return Array
@blah.data[0].class would return Hash

For some reason coming to Ruby from PHP I found distinguishing between Arrays and Hashes took me a long time, probably because in PHP an array is kind of both.

prom candy
Dec 16, 2005

Only I may dance
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.

prom candy
Dec 16, 2005

Only I may dance
So we're talking about the case where "!@message.valid? && signed_in?" right?

Maybe my memory is wrong but I didn't think render :template ran the controller action. How is @messages getting set for the view? Or are you just calling @user.messages directly in the view?

prom candy
Dec 16, 2005

Only I may dance
Is your view code references @messages or @user.messages? Actually can you paste your whole view (or at least the relevant parts)?

Edit: use gist.github.com to save the table-breakage

prom candy fucked around with this message at 21:05 on Mar 21, 2012

Adbot
ADBOT LOVES YOU

prom candy
Dec 16, 2005

Only I may dance
You're going in the right direction, accepts_nested_attributes_for is a good way to build little sub-forms for your child elements.

Try this: http://railscasts.com/episodes/196-nested-model-form-part-1

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply