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
Keevon
Jun 11, 2002
You know maybe instead of being an angry nerd and writing your paper about how poorly notch wrote a multi million dollar game you could try being productive and write your own game but properly and show him whats what:qq:

Colorblind Pilot posted:

Second question that's sort of related: if you want a session to expire after 1 day, what's stopping the user from manipulating the cookie that stores the session hash to make it never expire? Since it seems like no session data is stored server-side, does your app really have no control over it?

If I remember correctly, the cookie includes a signature, which is created using a secret that you define on the server side. If anything is changed about the cookie, the signatures won't match, and it will be considered invalid and the session ignored. This is incredibly important since if a user was able to change the cookie on their whim it opens you up to a whole host of attacks.

Adbot
ADBOT LOVES YOU

ChadyG
Aug 2, 2004
egga what?

8ender posted:

Just to be sure Apache + Passenger is the preferred setup for new Rails deployments right? Is it really as easy as just uploading the project files?

There is a restart.txt that needs to be touched(modified) to let the server know it needs to reload your project. Otherwise yeah, it's that easy.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
In the application I'm working on before showing things like last names, email addresses, etc. I have it set up so that privacy settings are assessed that ensure a user actually wants their email address or last name to be visible. The problem is that often a whole unnecessary query that traverses backwards through the user model from within the privacy model is run: "self.user".

Is there a way to magically give the user object into the privacy model when I reference the privacy object so it doesn't have to traverse backwards?

Having trouble solving this logic experiment, or else there is something very simple I just don't see!

code:
class User < ActiveRecord::Base
  belongs_to :privacy, :dependent => :destroy

  def display_name
    return self.firstname unless self.privacy.show_lastname?
    self.full_name
  end
end
code:
class Privacy < ActiveRecord::Base
  has_one :user

  def show_lastname?
    [false, true, self.determine_friend?][self.perm_lastname]
  end
  
  def determine_friend?
    return true if self.user.id == current_user.id
    return false unless relationship = self.user.relationship(current_user)
    relationship.is_friend?
  end
end

Evil Trout
Nov 16, 2004

The evilest trout of them all

Nolgthorn posted:

In the application I'm working on before showing things like last names, email addresses, etc. I have it set up so that privacy settings are assessed that ensure a user actually wants their email address or last name to be visible. The problem is that often a whole unnecessary query that traverses backwards through the user model from within the privacy model is run: "self.user".

Is there a way to magically give the user object into the privacy model when I reference the privacy object so it doesn't have to traverse backwards?

Having trouble solving this logic experiment, or else there is something very simple I just don't see!

code:
class User < ActiveRecord::Base
  belongs_to :privacy, :dependent => :destroy

  def display_name
    return self.firstname unless self.privacy.show_lastname?
    self.full_name
  end
end
code:
class Privacy < ActiveRecord::Base
  has_one :user

  def show_lastname?
    [false, true, self.determine_friend?][self.perm_lastname]
  end
  
  def determine_friend?
    return true if self.user.id == current_user.id
    return false unless relationship = self.user.relationship(current_user)
    relationship.is_friend?
  end
end

I had a similar issue once and I was able to do something like this (btw, you are using the self keyword way more than you should have to, it's usually unnecessary.)

code:
def display_name
  p = privacy
  p.user = self
  return self.firstname unless p.show_lastname?
  self.full_name
end
Having said that, it uglies up your code a lot, and I would guess it's not usually worth it unless you've found it to be a huge performance problem. Especially since ActiveRecord has a query cache, and if you previously retrieved the user by id it will not query the database a second time to do it in most cases, even if it shows up on your console as an extra query. (Look for the word cache beside it.)

Devian666
Aug 20, 2008

Take some advice Chris.

Fun Shoe
I've been trying to try out hobo but I'm gettting a lot of issues trying to connect to SQL. I'm sure I have something configured incorrectly or something is too new and is causing the problem. I literally can't get hobo to work at this stage.

dark_panda
Oct 25, 2004

Nolgthorn posted:

In the application I'm working on before showing things like last names, email addresses, etc. I have it set up so that privacy settings are assessed that ensure a user actually wants their email address or last name to be visible. The problem is that often a whole unnecessary query that traverses backwards through the user model from within the privacy model is run: "self.user".

Is there a way to magically give the user object into the privacy model when I reference the privacy object so it doesn't have to traverse backwards?

Having trouble solving this logic experiment, or else there is something very simple I just don't see!

I think you may be looking for the :inverse_of functionality in ActiveRecord. See http://mediumexposure.com/better-memory-associations-inverseof/ for details, but it basically tells an association to avoid some unnecessary queries by assuming that if an association is the inverse of another association that it is safe to use the originating object if it has already been fetched from the database rather than trying to re-fetch it.

To use it in your case, just modify your models thusly:

code:
class User < ActiveRecord::Base
  belongs_to :privacy, :dependent => :destroy, :inverse_of => :user
end

class Privacy < ActiveRecord::Base
  has_one :user, :inverse_of => :privacy
end
Before, you should notice three queries for the following code:

code:
User.first.privacy.user
One query to fetch the User, another to fetch the Privacy from the User, then a third to fetch the User again from the Privacy via users.privacy_id. After using :inverse_of, you should find that only the first two queries are run and the third is unnecessary.

This feature is completely undocumented in the Rails API documentation, so be wary of that. It is however mentioned very briefly in the ActiveRecord CHANGELOG, so one wonders why it has yet to be documented. There may be some bugs involving its use perhaps that have kept it from becoming a documented feature or something, but in any event I believe it will provide the solution you're seeking.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Wow, thank you dark_panda. That is pretty much exactly what I was looking for, I can go back and clean up some of my code now.

While Rails is using id columns to look up relational objects, the User object is exposed to my users through a different unique identifier. So the user objects weren't being retrieved by id most of the time, therefore are not getting cached.

Is there a patch to make this whole inverse_of functionality on by default in the works currently? Is it memory expensive, too much bloat?

dark_panda
Oct 25, 2004

Nolgthorn posted:

Wow, thank you dark_panda. That is pretty much exactly what I was looking for, I can go back and clean up some of my code now.

While Rails is using id columns to look up relational objects, the User object is exposed to my users through a different unique identifier. So the user objects weren't being retrieved by id most of the time, therefore are not getting cached.

Is there a patch to make this whole inverse_of functionality on by default in the works currently? Is it memory expensive, too much bloat?

There's a ticket on the Rails Lighthouse with a patch for automatically figuring out inverse associations here:

https://rails.lighthouseapp.com/projects/8994/tickets/3760-make-ar-guess-the-inverse-of-an-association-if-not-specified

I doubt we'll see the patch merged into Rails 2.3.x for it any time soon, though, seeing as the feature itself hasn't been documented yet. The feature itself is a back-port from the Rails 3 branch, so it will be better supported in Rails 3.

Since AR is going to be storing stuff in memory as it retrieves it from the database, if anything this would probably lead to less memory use, although you'd have to profile some requests to know for sure. I haven't gotten around to utilizing the feature yet myself so I haven't profiled anything, but I would imagine you'd see a performance improvement all things being equal given the savings in database queries and presumably some savings in object creation.

Also, note that the feature itself was added in Rails 2.3.6 and isn't available at all before that version.

Pardot
Jul 25, 2001




There's some awesome new Enumberable methods in 1.9.2 http://rbjl.net/27-new-array-and-enumerable-methods-in-ruby-1-9-2-keep_if-chunk

Enumerable — The Best Module™

8ender
Sep 24, 2003

clown is watching you sleep
I'm having a little trouble planning out a few models with Activerecord. I need to setup something like this

Company (has many) > Employees

But some employees will also potentially have many other employees directly reporting to them and I need to track that. I thought of setting up a simple "Manager" model but the organizational structure can be vastly different from company to company. So it could look like this:

Bob, Frank and Sally (report to) > Sam (reports to) > James and Dan (reports to) > Albert

And I need to somehow keep track of these associations in a reasonable manner while also letting users add, remove and modify the employee associations.

I'm stumped here and I really shouldn't be because I know how I'd do this with Java and some tables. I guess I'm a little too new to Rails to visualize how I'm going to pull this off properly with Activerecord. Any ideas?



Edit: Oh for christ sakes this exact scenario is an example in the online (http://guides.rubyonrails.org/association_basics.htm)l Activerecord associations guide. :suicide:

8ender fucked around with this message at 20:49 on Jul 6, 2010

Mrs. Wynand
Nov 23, 2002

DLT 4EVA

Pardot posted:

There's some awesome new Enumberable methods in 1.9.2 http://rbjl.net/27-new-array-and-enumerable-methods-in-ruby-1-9-2-keep_if-chunk

Enumerable — The Best Module™

Yay for combinations/permutations... they are remarkably handy.

Devian666
Aug 20, 2008

Take some advice Chris.

Fun Shoe

Mr. Wynand posted:

Yay for combinations/permutations... they are remarkably handy.

That could be incredibly useful for some of the stuff I'm working on.

NotShadowStar
Sep 20, 2000
Holy gently caress, the cyclical generators would have been really loving handy when I was learning abstract algebra for the first time.

(I didn't know Python/list comprehensions at that time)

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
Does anyone have any tricks for sanitizing and parsing international phone numbers that may be in no particular format? I've checked GitHub and Rubygems for libraries and the best one seems to be http://github.com/carr/phone, however, it still chokes with a lot of my data. Any recommendations that don't have me writing tons of custom regexe[s|n]?

rugbert
Mar 26, 2003
yea, fuck you
My boss wants me to create a Ruby mailer thats sent in variables from facebook via ajax but Ive never touched rails in my life. Anyone know of a good tutorial to get me though this?

8ender
Sep 24, 2003

clown is watching you sleep

rugbert posted:

My boss wants me to create a Ruby mailer thats sent in variables from facebook via ajax but Ive never touched rails in my life. Anyone know of a good tutorial to get me though this?

Some good places to start:
http://wiki.developers.facebook.com/index.php/User:Using_Ruby_on_Rails_with_Facebook_Platform
http://guides.rubyonrails.org/action_mailer_basics.html

and the absolute first place to start is learning Ruby:
http://mislav.uniqpath.com/poignant-guide/

CHRISTS FOR SALE
Jan 14, 2005

"fuck you and die"
Has anyone here worked with XMPP? I'm developing an application for a client that requires microblogging and eventually instant messaging. I definitely want to use XMPP for IM, and I am currently reading the book Professional XMPP Programming with JavaScript and jQuery and coming to realize that perhaps XMPP would be a great solution for the microblogging aspect as well, due to its near-real-time nature. I do have a few questions though...

1.) If you have worked with XMPP, what did you use it for? What problems did you encounter? How did you solve those problems?

2.) I already have a primitive "status update" microblogging thing going on right now for prototyping reasons, but it's all working off of one DB table...which I know is not going to be scalable in the future. I'd like to retain that RESTful design pattern, and still work off the User::StatusesController, but this Professional XMPP book got me real interested in Strophe handling all communication between jQuery/JS and the XMPP server. Can I have my cake and eat it too?

3.) How do I facilitate this communication? Do I get an XMPP server or something? I have the ability to set one up myself if so.

CHRISTS FOR SALE fucked around with this message at 15:36 on Jul 28, 2010

dizzywhip
Dec 23, 2005

What would be the best way to have a one-off piece of data in the database? Things like a little blurb at the top of the main page or the content of an "about" page -- I'd like to be able to edit that kind of stuff from the page itself, so I don't want to hard code it into my views. But it also seems kind of silly and messy to create an entire model and controller for little stuff like that.

Is there a recommended method for doing stuff like this?

Hammertime
May 21, 2003
stop

Gordon Cole posted:

What would be the best way to have a one-off piece of data in the database? Things like a little blurb at the top of the main page or the content of an "about" page -- I'd like to be able to edit that kind of stuff from the page itself, so I don't want to hard code it into my views. But it also seems kind of silly and messy to create an entire model and controller for little stuff like that.

Is there a recommended method for doing stuff like this?

Honestly ...

I'd just create a simple model (pk id, string name and a blob/text for the content, plus an int version column could be useful) + controller. Using a rails model generator really doesn't take long "ruby script/generate model page_content name:string content:text version:int", and poke through "map.resources :page_content" in routes.rb.

Wrap the displaying of the content in a 'cache' block so it hits memcache instead of the database for drawing the fragment and you're almost there.

dizzywhip
Dec 23, 2005

Hmm, alright. I wasn't sure if there was a simpler way of doing it or not.

Thanks!

jonnii
Dec 29, 2002
god dances in the face of the jews

Gordon Cole posted:

Hmm, alright. I wasn't sure if there was a simpler way of doing it or not.

Thanks!

I use high_voltage (http://github.com/thoughtbot/high_voltage) for my static pages.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

jonnii posted:

I use high_voltage (http://github.com/thoughtbot/high_voltage) for my static pages.

+1 for this. High voltage is awesome.

dizzywhip
Dec 23, 2005

Ooh, interesting. I'll give that a try, thanks!

BrokenDynasty
Dec 25, 2003

I'm having a hell of a time getting the sorting/filtering of results that I want.

Basically I want to sort posts (ascending and descending) by title, date, author, and sections. The title/date/author are all columns in my post table, and section is a reference (section has_many :posts). I heard SearchLogic was good but it's not rails 3 ready and I'm developing on 3.

I'm a few hours into trying to crank this out myself but I'm at wits end, are there any descent alternatives to SearchLogic that are Rails 3 ready?

NotShadowStar
Sep 20, 2000
What's wrong with normal SQL ordering? Rails 3 query interface makes it really easy.

http://edgeguides.rubyonrails.org/active_record_querying.html#ordering

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe
This is going to sound rather strange, but i'm bored and i fancy writing a rails app that some people will actually download and use. Anyone got any suggestions?

Hammertime
May 21, 2003
stop

NotShadowStar posted:

What's wrong with normal SQL ordering? Rails 3 query interface makes it really easy.

http://edgeguides.rubyonrails.org/active_record_querying.html#ordering

Pretty much. While in theory joining and sorting isn't always guaranteed when sorting a joined table, it works fine for the post->section structure you describe.

I assume you're sorting by section name when you talk about sorting by section.

Old school AR:
code:
# by post title
Post.all(:include => :section, :order => "posts.title ASC")

# by section
Post.all(:include => :section, :order => "sections.name ASC")
The only case that ActiveRecord never covered sufficiently was joining tables and applying a named scope to the outer join. You could kinda fix this by creating an extra association to cover it, but then that association couldn't take arguments. This has since been solved in AREL though.

Otherwise, ActiveRecord has you covered, and it definitely has you covered for the situation you describe.

plasticbugs
Dec 13, 2006

Special Batman and Robin
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

8ender
Sep 24, 2003

clown is watching you sleep

plasticbugs posted:

I had a column in my model named "Type".

If its any consolation I've done that a bunch of times and each time it stops me dead for a while until I figure out what I did.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

plasticbugs posted:

I had a column in my model named "Type".

I've done this or similar a lot with Rails.

One time it was a little obscure one and a problem didn't crop up with it until weeks later with some weird insane error on one specific specialized action. Now I use a thesaurus to pick most of my names.


A couple of current favourites are:

Crowd, Stream, Disclosure, Party, Participant, Actor, Candidate, Rival.

NotShadowStar
Sep 20, 2000
code:
Class Butts < ActiveRecord::Base
inheritance_column "type_of"
end

Oh My Science
Dec 29, 2008
Does anyone know of a good tutorial for installing Authlogic in Rails 3? I know how to generate the files and everything, I just keep getting errors.

Has anyone here done this yet?

for reference I followed: http://github.com/binarylogic/authlogic_example

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I believe the current rails 3 auth favourite is devise. http://github.com/plataformatec/devise

IsotopeOrange
Jan 28, 2003

Rails 3 released today :)
Announcement

plasticbugs
Dec 13, 2006

Special Batman and Robin

Nolgthorn posted:

I've done this or similar a lot with Rails.

One time it was a little obscure one and a problem didn't crop up with it until weeks later with some weird insane error on one specific specialized action. Now I use a thesaurus to pick most of my names.


A couple of current favourites are:

Crowd, Stream, Disclosure, Party, Participant, Actor, Candidate, Rival.

I'm going to start using your thesaurus trick. This wasn't my first run-in with a 'protected' word.

NotShadowStar
Sep 20, 2000
Just rename the inheritance column to something that doesn't exist, it doesn't hurt AR at all. Although I do think that STI shouldn't be triggered unless you explicitly enable it on the model so that's never a problem in the first place.

kzin602
May 14, 2007




Grimey Drawer
This may be off topic a bit. But I have been looking for an inexpensive host for rails applications. Most shared hosting providers seem to have a very poor Ruby support..

I played with heroku, and it's awesome for fast bug testing and showing off a Work In Progress to another person.

I'd really like access to a low end cloud or virtual dedicated machine that gives me shell access so I can perform builds mess with config fikes and learn by breaking stuff. virtual machines are great because after i gently caress up my server it's easy to just re-provision.

So... Anybody know of a cheap provider for a person trying to expend their Web Skills?

manero
Jan 30, 2006

kzin602 posted:

This may be off topic a bit. But I have been looking for an inexpensive host for rails applications. Most shared hosting providers seem to have a very poor Ruby support..

I played with heroku, and it's awesome for fast bug testing and showing off a Work In Progress to another person.

I'd really like access to a low end cloud or virtual dedicated machine that gives me shell access so I can perform builds mess with config fikes and learn by breaking stuff. virtual machines are great because after i gently caress up my server it's easy to just re-provision.

So... Anybody know of a cheap provider for a person trying to expend their Web Skills?

linode.com has decent service. I recently switched to them from slicehost since the linode 512M slice is the same price as a 256M slice at slicehost.

E: Or just use vmware or virtual PC locally with an ubuntu server install.

NotShadowStar
Sep 20, 2000
What's wrong with Heroku for deployment and breaking poo poo on your own stuff?

Adbot
ADBOT LOVES YOU

Pardot
Jul 25, 2001




Seriously heroku is the best: http://justuse.heroku.com/kzin602

(I also use them to hose my real site)

Pardot fucked around with this message at 22:10 on Dec 8, 2013

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