Search Amazon.com:
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 $3,400 per month for bandwidth bills alone, and since we don't believe in shoving popup ads to our registered users, we try to make the money back through forum registrations.
«144 »
  • Post
  • Reply
Evil Trout
Nov 15, 2004

Despite rumors to the contrary, I do not love cocks.

bitprophet posted:

So, um. Why would I want to use Kawaii instead of, you know. A terminal window? This reminds me of Campfire's reinvention of IRC because "it wasn't a Web site" (and then of course a client-side app, Pyro, to interface with the Web site, making it even more ...)

That said, I'm sure it was fun to make, and it looks neat, so good job at any rate!

Oh, it's because when you type in any commands that are longer than say, two console lines, the output is ridiculously hard to read.

For example, I'd rather look at this:


Click here for the full 780x376 image.


Than this:



(that's the same command in Kawaii and a script/console)

Adbot
ADBOT LOVES YOU

bitprophet
Jul 22, 2004



OK, that's pretty cool

Does Ruby have a pretty-print module of any kind? I haven't done much Rails work lately but I do know that the typical inspect output of objects in the Ruby console struck me as overly verbose and/or poorly formatted.

Python, for comparison, tends to print out very little info unless you ask for it (typically just the class name and a memory ID) or if the object in question has a human-readable string rep (most do, unless the dev who made the class in question was a lazy arse).

E.g. Django specifically has a tradition of defining such a string rep method for all classes so it's easy to tell objects apart in the shell, so you'll do something like (making this a one liner for brevity, and let's say it's a Person class) def __unicode__(self): return "(%s) %s %s" % (self.id, self.first, self.last).

Aside from class instances' string reps, simple objects like dicts (hashes) and lists can have nested versions -- which are typically hard to read in the same way Ruby objects can be -- and for those there's a pprint module which lets you print things out in a nicer indented form.

Anyway, all that babble aside, I'm glad you pointed it out, I only looked at the first screenshot on the site so I wasn't aware of this extra functionality. I'm okay with wheel reinvention as long as one does something extra on top of it

Pardot
Jul 25, 2001



bitprophet posted:

Does Ruby have a pretty-print module of any kind?

yes

code:
>> a = %w[just a regular array whoo]
=> ["just", "a", "regular", "array", "whoo"]
>> b = [a, a*3, a]
=> [["just", "a", "regular", "array", "whoo"], ["just", "a", "regular", "array", "whoo", "just", "a", 
             "regular", "array", "whoo", "just", "a", "regular", "array", "whoo"], ["just", 
              "a", "regular", "array", "whoo"]]
>> require 'pp'
=> true
>> pp b
[["just", "a", "regular", "array", "whoo"],
 ["just",
  "a",
  "regular",
  "array",
  "whoo",
  "just",
  "a",
  "regular",
  "array",
  "whoo",
  "just",
  "a",
  "regular",
  "array",
  "whoo"],
 ["just", "a", "regular", "array", "whoo"]]
=> nil
edit: table breaking

Pardot fucked around with this message at Jul 21, 2008 around 01:12

Nolgthorn
Jan 29, 2001



I am about to try and create my first plugin out of a CMS I built from scratch (with the help only of awesome awesome attachment_fu) but I've never created a plugin before and am having troubles finding a full walkthrough of the process and possibilities.

The plugin will involve view helpers, controller methods and models. The CMS runs on a behind the scenes Admin interface with a different layout and a password. The front end needs to be highly configurable and the controller methods not so much so, this will be more or less an internal plugin that we are capable of making modifications to if need be.

My question is where do I go to learn about the best ways of doing this that is not out of date?

atastypie
Dec 2, 2000



I haven't read this particular PDF, but PeepCode has a Plugin Patterns doc available.

Nolgthorn
Jan 29, 2001



Making plugins is hard, would something this complex be better as a generator? Would that be any easier or more useful than having it as a plugin?

Carabus
Sep 30, 2002

Booya.

I have a problem which I feel is not very complicated but I am learning programming in addition to ruby and keep running into dead ends with various approaches.

Say I have an array/matrix = [[4.0, 231], [1.5, 423], [4.0, 2312], [5.0, 231], [3.5, 423], [4.0, 342]]. What is relevant is that there are two arrays with the element 423, each associated with different floating points. Same thing for 231.

I want to "combine" those arrays with the shared index, and average the associated float numbers. So the resulting array would be [[4.5, 231], [2.5, 432], [4.0, 2312], 4.0, 342]]. There will never be more than two subarrays with the same integer so it doesn't seem necessary to store store intermediate arrays in order to calculate averages.

I would appreciate some direction. There are a number of promising methods (each_index, inject) available but I can't construct the appropriate block to get anywhere. Multi-dimensional arrays are also confusing.

Carabus fucked around with this message at Jul 22, 2008 around 20:34

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come

Hopefully this sort of maybe helps.

code:
x = [[4.0, 231], [1.5, 423], [4.0, 2312], [5.0, 231], [3.5, 423], [4.0, 342]]
x = x.map{ |sub| 
  same = x.select{|c|c[1]==sub[1]} #get a new array of all the elements with this integer
 [same.inject(0){|sum,i|sum+=i[0]}/same.length.to_f, sub[1]] #make a new array of [avg(l),integer]
}.uniq  #remove dupes

dustgun fucked around with this message at Jul 22, 2008 around 22:24

Pardot
Jul 25, 2001



Nolgthorn posted:

Making plugins is hard, would something this complex be better as a generator? Would that be any easier or more useful than having it as a plugin?

Having plugins that have views and stuff is hard in rails. There is something called plugin engines that do that, but it got some hate in 2006 from dhh, and I'm not sure what the current state of the project is.

Generators could work, it really depends on how complex what you're adding is going to be. It seems like it would be a pain.

Merb has something called parts slices, which do exactly this, and it's built into the framework. I've not used them myself though. If you're going to have more than one of these plugins, it may be worth rewriting to merb.

---

This has more or less become the ruby megathread. What do people think of getting the title renamed?

Pardot fucked around with this message at Jul 24, 2008 around 03:27

Carabus
Sep 30, 2002

Booya.

dustgun posted:

Hopefully this sort of maybe helps.
Wow thanks, I wasn't expecting that. And it's easy to follow.

Nolgthorn
Jan 29, 2001



Pardot posted:

Having plugins that have views and stuff is hard in rails. There is something called plugin engines that do that, but it got some hate in 2006 from dhh, and I'm not sure what the current state of the project is.
Thanks for this suggestion.

I went to the website and it's nothing but the poor developers overly defending the project like crazy for pages and pages and pages, geez. I'm so taken back now I'm not even sure what to do to start using the drat thing. All of the tutorials that are being linked to seem to be down, it looks like a nice project, being able to put a generator into a plugin sounds just the ticket.

I'll try and investigate further. I wish that the developers of Rails engines spent more time writing documentation than they did actually listening to all this criticism, sometimes the Rails community baffles me.

Nolgthorn fucked around with this message at Jul 23, 2008 around 01:41

Pardot
Jul 25, 2001



Yeah, I never promsied it'd be pretty Keep us updated on what you decide.

Carabus posted:

Wow thanks, I wasn't expecting that. And it's easy to follow.

http://www.ruby-doc.org/core/classes/Enumerable.html Learn all the stuff enumerable buys you. This mixin has all of my favorite ruby methods, by far.

j4on
Jul 6, 2003
I fix computers to pick up chicks.

Let's say you work in a "fast and dirty" coding environment (news website) where most projects go from design to publish in under a week, sometimes only one day, and usually involve only one coder. Maintenance on old code is rarely needed, but deadlines absolutely can not be missed: if breaking the MVC paradigm will save one hour, by God break it. For these kind of small data-driven "mini-sites" is RoR still better than PHP? Or should I be lookng to learn something else entirely?

j4on fucked around with this message at Jul 23, 2008 around 06:54

Evil Trout
Nov 15, 2004

Despite rumors to the contrary, I do not love cocks.

j4on posted:

Let's say you work in a "fast and dirty" coding environment (news website) where most projects go from design to publish in under a week, sometimes only one day, and usually involve only one coder. Maintenance on old code is rarely needed, but deadlines absolutely can not be missed: if breaking the MVC paradigm will save one hour, by God break it. For these kind of small data-driven "mini-sites" is RoR still better than PHP? Or should I be lookng to learn something else entirely?

I would say yes, Rails is still better than PHP absolutely.

One you get set up properly you can just spawn and build new apps very quickly. With Phusion Passenger you can deploy just as easily too.

Nolgthorn
Jan 29, 2001



j4on posted:

Let's say you work in a "fast and dirty" coding environment (news website) where most projects go from design to publish in under a week, sometimes only one day, and usually involve only one coder. Maintenance on old code is rarely needed, but deadlines absolutely can not be missed: if breaking the MVC paradigm will save one hour, by God break it. For these kind of small data-driven "mini-sites" is RoR still better than PHP? Or should I be lookng to learn something else entirely?

I would recommend spending any downtime you have building something which can be reused to build what you most usually do instantly. That or switching to Django, if you can manage Django it was actually built for news websites by a large news website something or other and does everything you want fast.

Once you've defined what your database looks like all you need to do is build a front end, the administration portion is already done, fyi but I find it hard.

Nolgthorn
Jan 29, 2001



Re; Rails Engines
AGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
*falls over*
*twitches*

Thanks Rails community for doing everything in your power to prevent Rails Engines, which is exactly what I need from having been updated in a year so that it no longer works. That's great how something that obviously had a lot of time and effort put into it got flogged so badly that the developers finally said 'gently caress this' and gave up on it.

Rails is now much better for not having this plugin available to those who would have wanted it in the future.

I'll surely not be considering contributing a lot of my time and effort into a community if there is a risk my work will be responded to as if it were a youtube video.

manero
Jan 30, 2006



Nolgthorn posted:

Re; Rails Engines
AGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
*falls over*
*twitches*

Thanks Rails community for doing everything in your power to prevent Rails Engines, which is exactly what I need from having been updated in a year so that it no longer works. That's great how something that obviously had a lot of time and effort put into it got flogged so badly that the developers finally said 'gently caress this' and gave up on it.

Rails is now much better for not having this plugin available to those who would have wanted it in the future.

Merb has slices

http://brainspl.at/articles/2008/05/21/merb-slices

Nolgthorn posted:

I'll surely not be considering contributing a lot of my time and effort into a community if there is a risk my work will be responded to as if it were a youtube video.

I've been active in other open source communities, but something has always struck me oddly with the Rails community. I've wanted to contribute, but something has just turning me off in the end.

Getting back to the merb love, the source is really well organized and documented, and the community is much smaller, for better or for worse... the stuff merb/datamapper have going actually make me want to contribute back.

defmacro
Sep 27, 2005
Sensei Banzai

There doesn't seem to be a general Ruby thread, so I figured I'd ask my question here.

Is there a quick way in Ruby to create an HTTP proxy server that simply alters the HTML document a bit (going to run a few String#gsub()'s on it is all) before it is returned to the browser? I looked into using WEBrick as Webrick::HTTPProxyServer seemed to be exactly what I was looking for. Poking through the examples/API, I couldn't find a quick way to do something like document_body.gsub(blah, blah2), or even a way to get at the returned document. Googling brought me to mouseHole, but after installing 7 new gems without satisfying all the dependencies, I decided it might be better to try something a little lighter-weight.

Any suggestions?

skidooer
Aug 5, 2001


GT_Onizuka posted:

Poking through the examples/API, I couldn't find a quick way to do something like document_body.gsub(blah, blah2), or even a way to get at the returned document.
code:
require 'webrick/httpproxy'

proxy = WEBrick::HTTPProxyServer.new(
    :Port => 8080,
    :ProxyContentHandler => lambda { |request,response|
      response.body.gsub!(%r{<a href=".*?">(.*?)</a>},
        '<a href="http://goatse">\1</a>')
    }
)

trap('INT') { proxy.shutdown }
proxy.start
Watch out for compressed content.

TumTum
Jan 30, 2005
ASK ME ABOUT MY WANKING TECHNIQUES

Nolgthorn posted:

Re; Rails Engines
AGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
*falls over*
*twitches*

Thanks Rails community for doing everything in your power to prevent Rails Engines, which is exactly what I need from having been updated in a year so that it no longer works. That's great how something that obviously had a lot of time and effort put into it got flogged so badly that the developers finally said 'gently caress this' and gave up on it.

Rails is now much better for not having this plugin available to those who would have wanted it in the future.

I'll surely not be considering contributing a lot of my time and effort into a community if there is a risk my work will be responded to as if it were a youtube video.
I'm not sure what you mean, there is a version of the plugin available for Rails 2.1 and the lighthouse page for it doesn't seem death either.

Nolgthorn
Jan 29, 2001



I've tried all the versions... maybe I'm doing something wrong.

B1axident
Sep 13, 2007

I'm a man of intensity. Of cool, and youth, and passionately

Not sure if this is the best place to ask, but I can't search for a Merb thread!

But I've been coding in Rails for a while now, and am getting interested in Merb and have seen it mentioned a few times in this thread. So does anyone have any good tutorials for Merb. Also is Merb functional enough to actually use it to produce sites?

Pardot
Jul 25, 2001



B1axident posted:

Not sure if this is the best place to ask, but I can't search for a Merb thread!

But I've been coding in Rails for a while now, and am getting interested in Merb and have seen it mentioned a few times in this thread. So does anyone have any good tutorials for Merb. Also is Merb functional enough to actually use it to produce sites?

There was actually a merb thread a while back, but this thread is fine. The main site is at http://merbivore.com/ and that gives you instructions on getting it. If you'd rather go edge than gem merb, you want to pull from wycats branch.

Merb is a bit different, in that the only thing you actually need is core. You'll want to get more and some plugins, but you don't need them, necessarily.

A good site for learning that came up recently is http://merbunity.com/. The irc channel is good too. There are a few people using merb here, so this thread could probably answer some questions.

Also check out datamapper.

derdewey
Dec 3, 2004


I was the author of that dead Merb thread...

Merb is all kinds of awesome coming out of my 3 years of rails experience. The documentation isn't my favorite, I really don't like the interface, but if you've got a strong rails background you can guess/search for what you need.

It is indeed functional enough to produce sites. I have a site coming together with merb and it feels pretty zippy to me. It's alpha, but it's a survey system geared towards employee evaluations (although you can't really do that yet). But if you just want to click around and try it out http://www.tureus.com/ . The text is mostly LOL, so sorry in advance.

It uses memcache for sessions, REST for all the admin routes (which is super awesome). I use Sequel for the database stuff because I love the interface and it's easy to get help with/read the code. I decided against datamapper because I wanted something with less magic. It's also hosted on the goon-run Apisnetworks and was breeze-y to get going.

HIERARCHY OF WEEDZ
Aug 1, 2005



I'm trying to throw OpenID at Sinatra, and goddamn is it annoying. The documentation comes with no examples -- which I half feel is an appropriate way to approach the subject, since it's important to understand all of the steps you're going through -- but on top of that, the library is written in a Ruby-esque way... or even at all sometimes.

code:
# File lib/openid/extension.rb, line 14
    def get_extension_args
      raise NotImplementedError
    end
Well, poo poo, that's helpful. On the plus side, I have basic authentication working, but getting SReg extension stuff -- like just a username would be nice -- doesn't seem to be happening. However, it may be the OpenID provider I'm using - Flickr through Yahoo!. Does anybody know how I can find out explicitly if Yahoo! provides an SReg implementation? I don't get anything back in the "openid.signed" param that mentions SReg.

atastypie
Dec 2, 2000



Jumping in to say that if you haven't tried out Passenger/mod_rails... give it a go. It's pretty awesome: just create your vhost file for the application and everything works from there. No setting up mongrel clusters, no extra configuration, easily stop/start, no PID problems. Pretty straightforward to setup cap tasks of your own to do things like restart the application or disable a vhost.

savetheclocktower
Sep 23, 2004

You wait and see, Mr. Caruthers. I will be president! I'll be the most powerful president in the history of America. And I'm gonna clean up this country!

Panic! at the Fist Jab posted:

I'm trying to throw OpenID at Sinatra, and goddamn is it annoying. The documentation comes with no examples -- which I half feel is an appropriate way to approach the subject, since it's important to understand all of the steps you're going through -- but on top of that, the library is written in a Ruby-esque way... or even at all sometimes.

code:
# File lib/openid/extension.rb, line 14
    def get_extension_args
      raise NotImplementedError
    end
Well, poo poo, that's helpful. On the plus side, I have basic authentication working, but getting SReg extension stuff -- like just a username would be nice -- doesn't seem to be happening. However, it may be the OpenID provider I'm using - Flickr through Yahoo!. Does anybody know how I can find out explicitly if Yahoo! provides an SReg implementation? I don't get anything back in the "openid.signed" param that mentions SReg.

Annoyingly, a lot of major providers don't implement SReg. I don't know if Y! is one of them. But you could try one that definitely does implement SReg, like claimID, and compare the result.

drjrock_obgyn
Oct 11, 2003

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

atastypie posted:

Jumping in to say that if you haven't tried out Passenger/mod_rails... give it a go. It's pretty awesome: just create your vhost file for the application and everything works from there. No setting up mongrel clusters, no extra configuration, easily stop/start, no PID problems. Pretty straightforward to setup cap tasks of your own to do things like restart the application or disable a vhost.

Just wanted to +1 that. Also if you're on OS X check out the Passenger pref pane: http://www.fngtps.com/2008/06/putti...into-deployment to add it right in to system preferences.

Un-l337-Pork
Sep 9, 2001

Oooh yeah...



I'm extremely new to RoR (although I've been using cakePHP for sometime now, so I thought that the general architecture would be mildly similar). I'm running into a strange error that I don't understand.

I'm only working with one model (user) at this point. I'm trying to get basic user authentication to function, but I can't even get it to render my login page -- let alone log a user into the site.

Here is routes.rb:
code:
map.resources :users
map.connect 'login', :controller => 'users', :action => 'login'
users_controller (this has the generated index/show/new/create/update/ as well):

code:
def login
end
When I try to browse to /login, I get the following error:

"ActionController::MethodNotAllowed
Only get, head, post, put, and delete requests are allowed."

What the gently caress? Googling for this hasn't yielded anything that seems to be relevant to what I am trying to do. Any help is appreciated -- I'm about to say "gently caress it" and go back to CakePHP, but I don't want to do that.

Pardot
Jul 25, 2001



What you're going for is a named route. Try map.login '/login', :controller => 'users', :action => 'login'

However having a login method on UsersController isn't RESTful. It's common to have a sessions controller, and new gives a login form, create makes a session (logs in), and destroy logs out.

--- edit:

We had a really weird problem yesterday. We upgraded rails to 2.1 and rspec to 1.1.4. There are 3 tables that have plural names and plural models, and so the fixture files are plural too. Now any spec that had fixtures :all, rails would go looking for all of the classes based on the fixture file names to load them. It couldn't find these three because of the break in the naming convention. There is set_fixture_class on Test::Unit::TestCase which the rspec example inherit from, but this happens before that comes up.

The problem is that we have a huge number of specs, and to run a full suite, it would look for these 3 classes, 18 times per file, causing it to just print stack traces to the log for almost 2 minutes before running the specs. The specs ran fine, because the fixtures in question are just empty files to get rails to clear those tables in integration tests (which is a whole other reason why fixtures suck...).

The solution? Dependencies.loaded << "#{RAILS_ROOT}/class_name_without_an_s" in spec_helper to trick rails into thinking that it was already loaded, and that it didn't need to go looking for the nonexistent file.

Pardot fucked around with this message at Aug 2, 2008 around 18:26

wunderbread384
Jun 24, 2004
America's favorite bread

Pardot is right about it not being RESTful, but it's usually OK to put it on the Users controller if you have a small site. To do that:

code:
map.resources :users, :collection => {:login => :all}
That routes any of the usual HTTP verbs (get, post, put, delete) to the login method on the users controller. If you only want one, say GET, use :get rather than :all.

The named path for that will be login_users and the URI will be '/users/login'. The path is different than you said but that way it's clearer that login is a method that applies to a collection of users.

Un-l337-Pork
Sep 9, 2001

Oooh yeah...



wunderbread384 posted:

Pardot is right about it not being RESTful, but it's usually OK to put it on the Users controller if you have a small site.

Awesome -- thanks for the help. I'm sure you'll be seeing me around here as I learn more (and consequently get stumped).

luminalflux
May 27, 2005



I'm starting out with a project in Rails, and have a couple questions:

- Why doesn't references in create_table in my migration create a foreign key? Why do I have to run a "execute 'ALTER TABLE ...'? I thought the whole point of Rails was to not repeat myself.

- Why doesn't acts_as_list automatically add 'ORDER BY position' clause when calling Foo.find(:all)? I've said it's a list with a position column, why do I have to specify this again in my controller?

- How do I create functions like 'edit_thingies_path' used in link_to in my view for other controller methods? I spent a 3 hour train ride trying to figure this out but all I got were stack traces and I reverted to specifying the action and arguments manually.

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

Un-l337-Pork posted:

Awesome -- thanks for the help. I'm sure you'll be seeing me around here as I learn more (and consequently get stumped).

I normally have a sessions controller for login/logout.

Carabus
Sep 30, 2002

Booya.

So another noob question here, but more open-ended. I'd like to know if the method I have been using for constructing hashes from queries is particularly bad. It varies with the circumstances but here is a representative example:
code:
def self.profiles
  favs = Hash.new {|k,v| k[v] = [] }
  Favorite.find(:all, :select=> "profile_id, fan_id").each do |i|
    favs[i.fan_id.to_s] = [] if favs[i.fan_id.to_s] == nil
    favs[i.fan_id.to_s] << i.profile_id.to_i
  end
  return favs
end

manero
Jan 30, 2006



Carabus posted:

So another noob question here, but more open-ended. I'd like to know if the method I have been using for constructing hashes from queries is particularly bad. It varies with the circumstances but here is a representative example:
code:
def self.profiles
  favs = Hash.new {|k,v| k[v] = [] }
  Favorite.find(:all, :select=> "profile_id, fan_id").each do |i|
    favs[i.fan_id.to_s] = [] if favs[i.fan_id.to_s] == nil
    favs[i.fan_id.to_s] << i.profile_id.to_i
  end
  return favs
end

1) The block you're passing to Hash.new probably isn't *really* doing what you think it is.

2) If there are a lot of favorites, you could have a long query

3) Likewise, why bother instantiating ActiveRecords when you could use the raw connection?

wunderbread384
Jun 24, 2004
America's favorite bread

luminalflux posted:

I'm starting out with a project in Rails, and have a couple questions:

- Why doesn't references in create_table in my migration create a foreign key? Why do I have to run a "execute 'ALTER TABLE ...'? I thought the whole point of Rails was to not repeat myself.

The Rails folks don't believe in foreign keys so you probably won't ever see support for them in core. It's stupid, but one of the many downsides to Rails being "opinionated software" is when their opinion is wrong.

There's a bunch of plugins you can get that will do it, though, try Foreign Key Migrations.

quote:

- How do I create functions like 'edit_thingies_path' used in link_to in my view for other controller methods? I spent a 3 hour train ride trying to figure this out but all I got were stack traces and I reverted to specifying the action and arguments manually.

Depending on how these are routed they should be created automatically. If you use named routes you just refer to them by the name (no controller or anything), or if you use the :member or :collection options to map.resources it should be, for example, do_something_thingies_path. See: http://api.rubyonrails.org/classes/.../Resources.html for mapping resources and http://api.rubyonrails.org/classes/...er/Routing.html for named routes (among other things).

You can run "rake routes" to see a pretty good list of all your routes, including the named methods.

Carabus posted:

So another noob question here, but more open-ended. I'd like to know if the method I have been using for constructing hashes from queries is particularly bad. It varies with the circumstances but here is a representative example:
code:
code...

Is there any reason you're not using relationships to do this?

wunderbread384 fucked around with this message at Aug 5, 2008 around 02:06

skidooer
Aug 5, 2001


Carabus posted:

I'd like to know if the method I have been using for constructing hashes from queries is particularly bad.
Perhaps it is because it is only an example, but there is just something about that method that is telling me that you are thinking about it the wrong way. As far as I can tell your example would be better described as:
code:
Profile.find(:all, :include => :fans)
I'm assuming your models look something like so:
code:
class Profile < ActiveRecord::Base
  has_many :favorites
  has_many :fans, :through => :favorites
end

class Favorite < ActiveRecord::Base
  belongs_to :profile
  belongs_to :fan
end

class Fan < ActiveRecord::Base
  has_many :favorites
  has_many :profiles, :through => :favorites
end

wunderbread384 posted:

The Rails folks don't believe in foreign keys
That's not surprising. Foreign keys don't really make any sense in ActiveRecord.

I'm actually having a hard time figuring out why you would want to use foreign keys if you are using ActiveRecord as intended. If you are not using ActiveRecord as intended, surely one of the other ORM libraries would be better suited for your needs?

Carabus
Sep 30, 2002

Booya.

wunderbread384 posted:

Is there any reason you're not using relationships to do this?
You mean :include and such? I started off just using select and haven't had any use for anything else until joins were called for when displaying informative results like names efficiently. Running calculations on the hash only requires the ids (foreign keys) which the favorites table provides kindly.

So maybe a raw SQL statement would be more appropriate? Although I'm not sure how that would improve my weird amateurish-looking each method.

Adbot
ADBOT LOVES YOU

Pardot
Jul 25, 2001



wunderbread384 posted:

It's stupid, but one of the many downsides to Rails being "opinionated software" is when their opinion is wrong.

You have to admit though, that there are also a ton of benefits to opinionated code. Like not having pages and pages of configuration files and getting methods thrown in because you name things a certain way, and so on.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply
«144 »