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
Pardot
Jul 25, 2001




Physical posted:

Why did the Jedi version not work? What is the point of non-@ members, when are they appropriate?

It's ambiguious between setting a local varaiable and calling the #name= method. If you to call the method use self.name='mickey mouse'

Adbot
ADBOT LOVES YOU

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

`@name` becomes an instance variable, `name` will just be local to the `initialize` method, and will be garbage-collected when the method is finished executing.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

A MIRACLE posted:

`@name` becomes an instance variable, `name` will just be local to the `initialize` method, and will be garbage-collected when the method is finished executing.

It'll be out of scope. Garbage collection is an implementation detail.

Physical
Sep 26, 2007

by T. Finninho
Wow something awesome just happened. I made Rails crash in a spectacular way by inspecting f in a form_for....


It crashed my VM so hard I couldn't even scroll to read it. I was doing this twice, not realizing it. When I got it down to 1 time I was able to read it and realized it contained like everything about our rails app. Routes, config stuff, totally unrelated classes, it went on forever making my scrollbar like 5px high. It did give me insight into what the hell f is a little bit and it's apparent magic.

Physical
Sep 26, 2007

by T. Finninho
I have to populate a table with some initial application data, is it ok to do it with a migration?

Obsurveyor
Jan 10, 2003

Physical posted:

I have to populate a table with some initial application data, is it ok to do it with a migration?

Populating initial data is what seeds are for, not migrations.

Physical
Sep 26, 2007

by T. Finninho

Obsurveyor posted:

Populating initial data is what seeds are for, not migrations.
This is for a production database. Seed file will drop the current one will it not?

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled

Physical posted:

This is for a production database. Seed file will drop the current one will it not?
No, it will just run the seeds.rb code against the existing database.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

Don't put it in a migration. That poo poo is a nightmare to maintain, esp when you bring new devs on.

prom candy
Dec 16, 2005

Only I may dance
Just write your seed code in a way that it won't override existing data if possible.

Physical
Sep 26, 2007

by T. Finninho
Ruby code:
  def index
    #get the default settings
    @defaultsettings = FieldSetting.where("account_id = -1")
    super
    @field_settings = @field_settings + @defaultsettings
  end
Except when it gets to the view its as if I didn't combine the two. An inspect shows that it's working right in the controller. I'm not sure what I'm missing.

prom candy
Dec 16, 2005

Only I may dance
What does the super action do and why are you calling it in the middle of your method? Also why did you name one variable defaultsettings and one variables field_settings? That's just ugly.

Physical
Sep 26, 2007

by T. Finninho

prom candy posted:

What does the super action do and why are you calling it in the middle of your method? Also why did you name one variable defaultsettings and one variables field_settings? That's just ugly.
Inherit resources is why I'm calling super. Field_settings is what was scaffolded, defaultsettings is just there while I figure things out. But since I'm doing this after super, shouldn't it be irrelevant (I'm guessing somehow not).

prom candy
Dec 16, 2005

Only I may dance
Probably when you call super it's rendering the template, so anything that happens afterward is irrelevant.

MrDoDo
Jun 27, 2004

You better remember quick before we haul your sweet ass down to the precinct.
Is anyone at Rubyconf?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

MrDoDo posted:

Is anyone at Rubyconf?
Not this year :(

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


I didn't quit my job quite early enough.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

I'm going to try damned hard to make RailsConf in Portland next April.

Physical
Sep 26, 2007

by T. Finninho

A MIRACLE posted:

I'm going to try damned hard to make RailsConf in Portland next April.
Didn't even know RubyConf was a thing.

Doc Hawkins posted:

I didn't quit my job quite early enough.
For what?

Pardot
Jul 25, 2001




Physical posted:

Didn't even know RubyConf was a thing.

For what?

I'd assume soon enough to go to rubyconf.

Speaking of conferences, we just recently set the date for the next Waza. It was really great last year, check it out http://waza.heroku.com/

Big Nubbins
Jun 1, 2004
In ActiveRecord, Is there a good way to coalesce two (in this case) associations with the same model to form another, cleanly? If I lost you, here's what I'm trying to do:

I have a Transaction model that's acting as a sort of audit log for transactions of virtual currency within the software. The Transaction model has two polymorphic attributes: receivable and payable. Think of a receivable or payable as "an entity that can pay or receive currency". In this case, my User model has two associations with Transaction: credits where the User is a receivable and debits where the User is a payable. What I want to do is have a third association, transactions that coalesces credits and debits. I want it to be an association and not simply a method on User so I can chain and scope it.

I'm having a REALLY hard time writing an association definition that should read like "has many :transactions where User is either a receivable or a payable".

double sulk
Jul 2, 2010

This is probably a really stupid question but I can't figure out what's going on. I'm pretty sure that initially, when I went to run Rails locally for development, my assets would refresh upon page load if I updated my CSS. For example, I have a background image set right now that won't go away if I remove the code and refresh the page (or even restart the server, it seems like). I did precompile the code and push the code to Heroku, but even based on the research I did online, it seems as if rake assets:clean does nothing (as it just removes the public/assets folder).

I'm still running WEBrick in development mode as far as I can tell, but removing the line

code:
 config.assets.initialize_on_precompile=false 
doesn't do anything, nor does adding some of the various config.assets.____ lines mentioned in answers I've seen on StackOverflow and the like. What's the deal, here? I'm on Rails 3.2, and a lot of the things mention 3.1 assets. I've looked in the docs a bit, and "config.assets.compile = true" in development.rb doesn't seem to do anything.

Pardot
Jul 25, 2001




Have you read this article https://devcenter.heroku.com/articles/rails3x-asset-pipeline-cedar ?

double sulk
Jul 2, 2010


That didn't seem to help me as much as

http://edgeguides.rubyonrails.org/asset_pipeline.html#local-precompilation

The non-edge version has a bunch of stuff going on, but this one line in the link above seems to resolve the problem:

code:
config.assets.prefix = "/dev-assets"

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Shame Boner posted:

I'm having a REALLY hard time writing an association definition that should read like "has many :transactions where User is either a receivable or a payable".
Make a method, not an association:
Ruby code:
def transactions
  credits + debits
end

double sulk
Jul 2, 2010

Okay, I have a new problem! I tried asking in the Rails IRC but no one seems to want to help, though it's likely a pretty basic relationship question I'm not fully understanding.

https://gist.github.com/0fe5cf9093b21f4f632f

I apologize if the code is really bad/adds a few things that don't need to exist as I'm still trying to get used to how Rails works. I want to have comments for each post that is made on my website -- and the logic I have is that users have many comments, and I suppose that the posts have many comments through the users. I do have that relationship added (though it doesn't seem to be doing anything in particular), and I feel as if all of my table references are correct (though I haven't added anything to users as I'm using devise and didn't feel like I needed to add anything as the post is a separate object itself). I can create comments on posts in development testing, but it appears that the created objects are not creating a post_id property for the post reference, and I cannot find comments for a given post as a result.

If anyone can tell me what I'm doing wrong, I'd really appreciate it. I've read some documentation and a lot of searching, but a problem I notice with Rails is that everything seems to move so quickly development-wise that there is a lot of outdated information as well as a lot of ways to accomplish different things in Rails itself, which leads to an apparently lack of "definitive" answers. I do at least appreciate the work I've done with it so far because while it has some magic to it, I've also had to dig into some documentation and figure out how a lot of things work (in this case, I'm just having trouble).

Obsurveyor
Jan 10, 2003

gucci void main posted:

Okay, I have a new problem! I tried asking in the Rails IRC but no one seems to want to help, though it's likely a pretty basic relationship question I'm not fully understanding.

You don't include the code that actually assigns comments to users/posts. I'm guessing that's where the issue lies. Your setup isn't magic, it won't do this for you. If you're not doing something like:

code:
comment.user = current_user
comment.post = post
comment.save
that's your issue.

double sulk
Jul 2, 2010

I added the posts/comments controllers. Pardon some of the scaffolding/messy code, again, which will get fixed soon. As of right now, I can show all of the comments in general. I was also saving, at the very least, the user_id variable, so it can understand which account is making the comment. I'm not really sure where/when I should be passing in the post_id, so that's the biggest issue. Any way I've tried to do it in the posts_controller (since in theory I should be able to edit that attribute when the post page is loaded), I get an "undefined method 'post_id'" or "no post_id column" error, indicating that it doesn't exist, even though as far as I understand, I've defined it.

If I show the attributes on a comment created, I get the following:
{"id"=>1, "content"=>"asdfagasdfadsg", "posted"=>Sat, 03 Nov 2012 20:36:53 UTC +00:00, "user_id"=>1, "created_at"=>Sat, 03 Nov 2012 20:36:53 UTC +00:00, "updated_at"=>Sat, 03 Nov 2012 20:36:53 UTC +00:00}

Obsurveyor
Jan 10, 2003

gucci void main posted:

I'm not really sure where/when I should be passing in the post_id, so that's the biggest issue. Any way I've tried to do it in the posts_controller (since in theory I should be able to edit that attribute when the post page is loaded), I get an "undefined method 'post_id'" or "no post_id column" error, indicating that it doesn't exist, even though as far as I understand, I've defined it.

http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

code:
class Post < ActiveRecord::Base
  has_many :comments, :through => :user
end
This is your problem, I believe. You wouldn't use a :through relationship in this instance because a user does not link a comment to a post. A post's relationship to a comment and a user's relationship to a comment are independent, unless you really only want to show the comments to a post by the user that posted the post(and no one else).

kitten smoothie
Dec 29, 2001

MVC design question here.

Suppose we're modeling a restaurant order system. We've got Tabs, which would have attributes for table number and waiter. We'd have DrinkOrder, and FoodOrder, which would inherit from TabLineItem, and a Tab would have many TabLineItems.

I want the "show" view for a Tab to have two forms, one for drink order, and one for food order. Am I correct in having controllers for food orders & drink orders, and those forms should be posting to the 'create' routes within those? Once something is successfully created, it should be bouncing them back to the "show" view for that given Tab?

If that's the case, how do I handle the instance where there are validation errors and I need to render the "show" view for the Tab with the fields filled in and errors highlighted?

Obsurveyor
Jan 10, 2003

kitten smoothie posted:

Once something is successfully created, it should be bouncing them back to the "show" view for that given Tab?

If that's the case, how do I handle the instance where there are validation errors and I need to render the "show" view for the Tab with the fields filled in and errors highlighted?

This is a bit vague, so I'm probably assuming too much: You can't successfully create something and then fail validation. In fact, if you're saving the TabLineItems under the Tab, the Tab is going to have to already exist(i.e. be saved) to save those. The Tab sounds like it could be extremely simple with just a table number, waiter and possibly a paid bool.

kitten smoothie
Dec 29, 2001

Obsurveyor posted:

This is a bit vague, so I'm probably assuming too much: You can't successfully create something and then fail validation. In fact, if you're saving the TabLineItems under the Tab, the Tab is going to have to already exist(i.e. be saved) to save those. The Tab sounds like it could be extremely simple with just a table number, waiter and possibly a paid bool.

Sorry, I should clarify. The "Show" view for the Tab would have the waiter's name, table number, whatever other attributes about a Tab that aren't specific to the line items. I need two forms on that screen, one for adding a FoodOrder, one for a DrinkOrder, so through these you'd be adding all the line items that make up a dinner tab.

There'd also be a table that listed the order line items associated with the Tab.

If a user should screw up entering a FoodOrder and fail validation, I want the user to get bounced back to the Tab view they were at before, just with the bad fields highlighted.

kitten smoothie fucked around with this message at 01:51 on Nov 5, 2012

Obsurveyor
Jan 10, 2003

kitten smoothie posted:

Sorry, I should clarify. The "Show" view for the Tab would have the waiter's name, table number, whatever other attributes about a Tab that aren't specific to the line items. Then there'd be two forms, one for adding a FoodOrder, one for a DrinkOrder, so through these you'd be adding all the line items that make up a dinner tab.

There'd also be a table that listed the order line items associated with the Tab.

If you should screw up entering a FoodOrder and fail validation, I want the user to get bounced back to the Tab view they were at before, just with the bad fields highlighted.

Assuming you're following the Rails way(using resources in the routes), wouldn't your FoodOrder url look something like:

/tab/:tab_id/food_order/new

In the controller(probably not 100% correct, just going off the top of my head):

code:
# app/controllers/tab/food_order.rb

class FoodOrderController < ApplicationController
  def new
    @tab = Tab.first params[:tab_id]
  end

  def create
    @food_order = FoodOrder.new params[:food_order]

    if !@food_order.valid?
      render :new
    else
      @food_order.save

      redirect_to tab_edit_path @food_order.tab.id
    end
  end
end
Where the food_order new view renders the tab view partial for the info at the top and the form for the new foodOrder.

Obsurveyor fucked around with this message at 02:06 on Nov 5, 2012

kitten smoothie
Dec 29, 2001

Obsurveyor posted:

Assuming you're following the Rails way(using resources in the routes), wouldn't your FoodOrder url look something like:

/tab/:tab_id/food_order/new

In the controller(probably not 100% correct, just going off the top of my head):

code:
# app/controllers/tab/food_order.rb

class FoodOrderController < ApplicationController
  def new
    @tab = Tab.first params[:tab_id]
  end

  def create
    @food_order = FoodOrder.new params[:food_order]

    if !@food_order.valid?
      render :new
    else
      @food_order.save

      redirect_to tab_edit_path @food_order.tab.id
    end
  end
end
Where the food_order new view renders the tab view partial for the info at the top and the form for the new foodOrder.

Ah, I was doing the first part of all that and routing things as you describe, but I didn't think to have the food_order new view basically reuse the guts of the tab view. I'll give that a go. Thanks!

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Backstory: I fix bugs and make enhancements to our Rails apps at work. We use a mix of Rails 1, 2, and 3 across about 15 apps! It's hell. We also use a mix-match of ERB, HAML, Prototype, jQuery, Backbone, and Spine depending on how new the app is. Not real familiar with the last two because those apps are still in QA and I haven't had much hands-on time.

Anyway, my question is what should I use to go about making some AJAX-y site with dynamic updates? I want to make a simple chat server, something like StackExchange has. I have no problem creating a messageboard-type application from scratch, I can set up the DB and the views and controllers and all that stuff. It's the interactiveness that gets me.

But it seems like there are 500 different templating systems and Javascript libraries, I honestly have no clue where to start. If you were going to make something like that where would you start?

Is this more on the Javascript side than the Rails side? It seems like I'll have some sort of event loop that's polling the server for messages every 3 seconds or whatever, and then adding each incoming message to the page, and then an input box that sends a message to the server (to then save in the DB)

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

IDK man I would honestly look into Node.js and websockets and stuff if you want to make a chat room. You could probably still hook it up to ActiveRecord if you want a sql log of the messages.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

A MIRACLE posted:

IDK man I would honestly look into Node.js and websockets and stuff if you want to make a chat room. You could probably still hook it up to ActiveRecord if you want a sql log of the messages.

I went through two Node tutorials yesterday, but I realized that's all server-side stuff, right? I'm not really having an issue with that end of it. I think I need to learn some more Javascript and it will make sense.

The Sweetling
May 13, 2005

BOOMSHAKALAKA
Fun Shoe

Bob Morales posted:

I went through two Node tutorials yesterday, but I realized that's all server-side stuff, right? I'm not really having an issue with that end of it. I think I need to learn some more Javascript and it will make sense.

If a chatroom is what you need, peep this Meteor.js tutorial. It definitely piqued my interest in the framework.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

The Sweetling posted:

If a chatroom is what you need, peep this Meteor.js tutorial. It definitely piqued my interest in the framework.

Thanks for the link I will watch it tonight!

Adbot
ADBOT LOVES YOU

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
I'm building a rails app which will be hosted on Heroku and pulls data from an RDS MySql connection. Note that this connection is in addition to the Postgres connection to the database that's the persistence layer for the rails app itself.

I've been able to authorize Heroku to connect to the RDS instance; however, it replaces CONFIG['DATABASE_URL'] with the mysql connection string, which is not a huge problem in itself.

The thing is, the value stored there is a Mysql2 connection string: e.g. mysql2://user:pass@....

I want to use the mysql2 gem to connect to this database, and Mysql2::Client.new takes a hash of parameters. Short of doing a regex to pull out these parameters into a hash, is there a way to connect using a connection string?

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