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
Lamont Cranston
Sep 1, 2006

how do i shot foam
I wonder if anyone could help me out with an issue I'm having.

I've just deployed an app using restful_authentication on edge rails (2.3.0) and it works perfectly in my development environment. I deployed it to my web server in production however, and it seemed that the session was not persisting between requests. I set up some loggers inside the current_user= method in AuthenticatedSystem and I could see that the session variable was being set, but then when the app redirected to the page that needed authentication, logged_in? returned false and there was nothing in the session. I could see that it set a cookie however.

I tried changing it to active_record_store, which worked, but now every few requests there's a pause of about 5 seconds before the page loads.

Does this ring any bells for anyone? I can't figure it out and Google's been worthless. I can provide more details upon request. Thanks.

Adbot
ADBOT LOVES YOU

Hop Pocket
Sep 23, 2003

Not necessarily a Rails question, but more of a Ruby / Module question. I recently wrote a module to try and wrap up some functionality that was duplicated across parts of our application. In doing so, I developed the module in isolation, creating public API methods along with private methods to help with the heavy lifting.

Once I mixed in the module into my controller, I found that a method that my controller defined was always taking precedence of the same named method in the module that was being mixed in. This makes sense given the nature of Ruby method dispatching. It starts at the receiver's class and proceeds up the Class/Module hierarchy, finding the first method that matches the name. Of course, it would find the method defined in the object's Class before it found the method in the mixed-in Module.

This made me really think twice about abstracting out functionality into Modules. I'd really love to have a way to have Ruby dispatch method calls first to the type from whence the method came instead of starting over with the normal dispatch routines coming from the object class itself.

This gist gives an example of what I'm talking about. #hello invokes the Person implementation of #saying rather than the Greeter method of #saying. #hello_two bypasses this through clunky method binding to execute the Module method rather than the original Class method.

Am I using Modules incorrectly here?

Pardot
Jul 25, 2001




Hop Pocket posted:

I found that a method that my controller defined was always taking precedence of the same named method in the module that was being mixed in

If you really wanted the module stuff to win, you can put the include at the end of the class. That's weird though, and probably misleading since people will expect the stuff in the class to win.

I'm not really understanding why if you didn't want the method to run you don't just delete it?

NotShadowStar
Sep 20, 2000

Hop Pocket posted:

Not necessarily a Rails question, but more of a Ruby / Module question. I recently wrote a module to try and wrap up some functionality that was duplicated across parts of our application. In doing so, I developed the module in isolation, creating public API methods along with private methods to help with the heavy lifting.

Once I mixed in the module into my controller, I found that a method that my controller defined was always taking precedence of the same named method in the module that was being mixed in. This makes sense given the nature of Ruby method dispatching. It starts at the receiver's class and proceeds up the Class/Module hierarchy, finding the first method that matches the name. Of course, it would find the method defined in the object's Class before it found the method in the mixed-in Module.

This made me really think twice about abstracting out functionality into Modules. I'd really love to have a way to have Ruby dispatch method calls first to the type from whence the method came instead of starting over with the normal dispatch routines coming from the object class itself.

This gist gives an example of what I'm talking about. #hello invokes the Person implementation of #saying rather than the Greeter method of #saying. #hello_two bypasses this through clunky method binding to execute the Module method rather than the original Class method.

Am I using Modules incorrectly here?

Use a nested module, and only include the top module. This is what you do when you write plugins to ensure no name collisions

http://wiki.rubyonrails.org/rails/pages/HowToWriteAnActsAsFoxPlugin

code:
module Clownfarts
  module Beefsteak
    
    class << self
      def duck_fuck
        "minced assholes"
      end
    end
    
  end
end

include Clownfarts

p Beefsteak.duck_fuck

Hop Pocket
Sep 23, 2003

NotShadowStar posted:

Use a nested module, and only include the top module. This is what you do when you write plugins to ensure no name collisions

http://wiki.rubyonrails.org/rails/pages/HowToWriteAnActsAsFoxPlugin

code:
module Clownfarts
  module Beefsteak
    
    class << self
      def duck_fuck
        "minced assholes"
      end
    end
    
  end
end

include Clownfarts

p Beefsteak.duck_fuck

This works, but my situation is a little different. I want to mix in instance methods on a controller, as in this case I want to be able to access the params and other methods/instance_variables from within my module code as if I were in the controller. So in that case, I'd need to simply include the Module and from there call module code.

I agree, though, that having in this case Clownfarts::Beafsteak as the receiver of #duck_fuck solves this problem, but I'm really looking to have the receiver of my module methods be an instance of ActionController::Base.

Hop Pocket
Sep 23, 2003

Pardot posted:

I'm not really understanding why if you didn't want the method to run you don't just delete it?

That's a very good question. I have inherited some code that I am trying to refactor, and part of what I'm doing right now is to break out some code into modules to try and get my class size down. My namespace collision is in an area of the code that I'm not even working on and so I was hoping to find a way to avoid colliding with it during this refactoring.

manero
Jan 30, 2006

Oh great, no more capistrano?

I hope the community picks up development. There's a decent flamewar going on over at Hacker News about it.

Jamis Buck posted:

However, please know that I am not available for questions about the code, or for advice on how to implement changes. I’m trying to cut as cleanly as I can. Any emails I get asking about the code will likely be ignored. I’m not trying to be rude; I’m just setting expectations.

Dropped like a ton of bricks. Capistrano is useful and all but gosh way to abandon.

"Let the community decide" sounds awfully a lot like "Let the market decide"

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

manero posted:

Oh great, no more capistrano?

I hope the community picks up development. There's a decent flamewar going on over at Hacker News about it.


Dropped like a ton of bricks. Capistrano is useful and all but gosh way to abandon.

"Let the community decide" sounds awfully a lot like "Let the market decide"

I think someone will most definitely pick it up. There are too many people using it (not even just in the Rails community) for it to die, really.

I think Jamis handled this poorly. He should have asked for help a lot earlier, and he could have switched to a "project manager" kind of role a long time ago, kind of like DHH with Rails. For whatever reason, Capistrano was always viewed as "his" project, and bug reports, patches, etc always went through him. That's why he got burnt out, and I think that burn out was completely avoidable.

That said, maybe Capistrano should die. It is pretty much a reimplementation of Rake, only less awesome. Maybe this will get people to consider alternatives like Vlad more seriously. There's no reason for a deployment solution to not use Rake. Jamis is a great developer, but his projects are always way too grandiose.

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!

Operation Atlas posted:

I think Jamis handled this poorly. He should have asked for help a lot earlier, and he could have switched to a "project manager" kind of role a long time ago, kind of like DHH with Rails. For whatever reason, Capistrano was always viewed as "his" project, and bug reports, patches, etc always went through him. That's why he got burnt out, and I think that burn out was completely avoidable.

Yeah, and this comment of his is telling:

Jamis posted:

@Nic, no, there are no other core contributors. I tried once to create something like that, but no one else seemed to have the “passion” or “vision”. Lots of people submitting patches (many of them quite good!), but no one demonstrating a real, general desire to dig into the internals. That’s kind of why I left it like I did—there really wasn’t any heir-apparent that the keys could be left to.

Now, some open-source projects are going to be lucky and get matched up with someone who wants to spend ten hours per week maintaining the drat thing because what the project does just so interesting to that person. But for the most part that's just not how poo poo works.

You can't run a big project like that with just one person, and you can't transition off a project by finding another dictator and dumping it in her lap. If it's big enough that you're getting burned out, you need to change the workflow — pick the part that you like doing most and get other people to help out with the rest.

In other words, if I need to hire a nanny, I'm not going to look for someone who's going to love my kid as much as I do; I'm going to look for someone who knows CPR and can make sandwiches.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
That was just the problem with Capistrano, it WAS NOT A BIG PROJECT it was made big by the guy writing it when really all it had to be was really simple. I'm glad it's gone, I hope it never gets worked on again and I hope myself or someone else finally gets off their rear end and makes something good, like that "The Destroyer" thing which was posted.

I hate Capistrano, through no personal or "emotional" response to the person who was building it, I just think that it is not useful and only gets used because people pile behind things regardless.

niralisse
Sep 14, 2003
custom text: never ending story

Nolgthorn posted:

I hate Capistrano, through no personal or "emotional" response to the person who was building it, I just think that it is not useful and only gets used because people pile behind things regardless.

It sure solved a lot of problems for me on a big PHP project that needed a drop-in deployment tool. Maybe rake could've done the job as well, but cap was much more approachable and focused. I'm sure something will replace it but for all the projects using it now it's going to make for a lot of unnecessary, redone work just to move to something maintained.

Lamont Cranston
Sep 1, 2006

how do i shot foam
Anyone know how I can dynamically get a list of the actions in a controller? From the console, I can do this:
code:
FooController.public_instance_methods(false)
#=> ["index", "another_action", "whatever"]
But from inside the controller, putting
code:
self.public_instance_methods(false)
yields an empty array. Any idea why or how I can get the above behavior within a controller?

skidooer
Aug 6, 2001

Lamont Cranston posted:

Anyone know how I can dynamically get a list of the actions in a controller?
Call the method after your methods have been defined. i.e.
code:
class FooController < ApplicationController
  public_instance_methods(false) # => []

  def index
  end

  public_instance_methods(false) # => ["index"]
end

Lamont Cranston
Sep 1, 2006

how do i shot foam

skidooer posted:

Call the method after your methods have been defined. i.e.
code:
class FooController < ApplicationController
  public_instance_methods(false) # => []

  def index
  end

  public_instance_methods(false) # => ["index"]
end

Hah wow, that was way simpler than I thought. Thanks!

Lamont Cranston
Sep 1, 2006

how do i shot foam
Well, after getting past the above problem, I finally finished up / extracted a plugin for search engine XML Sitemaps. There's one out there already but I didn't really like how it was installed and set up, I wanted something where I could just stick a method in my controller and have it be mapped. I have no clue if this will be useful for anybody else, but here it is:

http://github.com/tylercunnion/sitemap_xml/tree/master

I'll gladly accept any suggestions/patches. Let me know if you find it useful!

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Question; but good lord, let me try my best to explain it. :)


I have a users table and a conversations table. I've constructed a conversation_users table to keep track of the last time a user visited a given conversation and some assorted other info that relates the two.

What I want to do is eagerly load the information in this table depending on both which user is logged in and which conversations are currently being viewed. Right now my models simplified look like this:
code:
class User < ActiveRecord::Base
  has_many :conversations, :dependent => :nullify
  has_many :conversation_users, :dependent => :destroy
end

class Conversation < ActiveRecord::Base
  belongs_to :user
  has_many :conversation_users, :dependent => :destroy
end

class ConversationUser < ActiveRecord::Base
  # This table is used to keep a record of the last
  # time a user visited a given conversation
  belongs_to :user
  belongs_to :conversation
end
Right now I can see no way to eagerly load conversation_users except for either by all users of a given conversation or by all conversations of a given user.

What I'd ultimately like to do is add something like this to the Conversation model:
code:
class Conversation < ActiveRecord::Base
  belongs_to :user
  has_many :conversation_users, :dependent => :destroy
  has_one :conversation_user, :conditions => ["conversation_users.user_id = ?", current_user.id]
end
But that I cannot do, I'm certain I've just confused myself maddeningly. Please help!

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Nolgthorn posted:

What I want to do is eagerly load the information in this table depending on both which user is logged in and which conversations are currently being viewed. Right now my models simplified look like this:

If I'm understanding you correctly, I think this will work:

(Example for Conversations#Show action)

code:
Conversation.find(params[:id], :include => [:conversations_users], :conditions => {:user_id => current_user.id})
If that doesn't work, I apologize, I actually haven't been doing much Rails work lately. But it should be somewhere in the area.

If what you want is this stuff to be automatically loaded in the model, I think you're looking in the wrong place.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I have moved things to the controller like you suggest and added an extra method to the model. Thank you for your help, I'm concerned though it will start to get a lot messier as the project advances forward.

Here's what I've done:
code:
class ConversationsController < ResourceController::Base
  private
  def collection
    if current_user
      conditions = ["(conversation_users.user_id = ? OR conversation_users.user_id IS NULL)", current_user.id]
      @collection ||= end_of_association_chain.find(:all, :include => :conversation_users, :conditions => conditions)
    else
      @collection ||= end_of_association_chain.find(:all)
    end
  end
end

class Conversation < ActiveRecord::Base
  has_many :conversation_users, :dependent => :destroy
  def conversation_user
    self.conversation_users.first
  end
end
There will only ever be one conversation_user for any pairing of both user and conversation, there still doesn't seem to be a way to clarify that in rails. Currently it's all just being written out in custom methods that have a lot of checks and balances to eliminate a corrupt database for something that doesn't seem like it would be so uncommon a thing to need to do.

Something tells me I'm not doing this the rails way and I'd like to, unless this is the rails way and I'm just confusing myself.

Nolgthorn fucked around with this message at 23:00 on Mar 12, 2009

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Nolgthorn posted:

Something tells me I'm not doing this the rails way and I'd like to, unless this is the rails way and I'm just confusing myself.

Well, the "Rails Way" is has_and_belongs_to_many, but you can't use that because (if I read correctly) you have extra attributes you need to keep track of.

I really can't tell what you're trying to do, but you're doing it wrong. You're overthinking it. This is really quite a simple thing.

code:
class User < ActiveRecord::Base
  has_many :conversations, :dependent => :nullify
  has_many :conversation_users, :dependent => :destroy
end

class Conversation < ActiveRecord::Base
  belongs_to :user
  has_many :conversation_users, :dependent => :destroy
end

class ConversationUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
end

class ConversationsController < ApplicationController
  def show
    @conversation = Conversation.find(params[:id])
    @conversation_user = @conversation.conversation_users.find(:first, :conditions => {:user_id => current_user.id})
  end
end
That's really all you need. No methods on the model. No extra methods in the controller. Just finding stuff. And definitely no SQL in the conditions.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
that is what im doing.

The problem is i want to eagerly load the table for items on the index, which isn't really a problem... just a concern that it's not the best way to do it. Thats all, and Im obviously doing something that isnt common after all so no worries.

:(

skidooer
Aug 6, 2001

Operation Atlas posted:

Well, the "Rails Way" is has_and_belongs_to_many, but you can't use that because (if I read correctly) you have extra attributes you need to keep track of.
The "Rails Way" would be to use has_many :through, which is essentially what he is doing, save actually defining the through association.

code:
class User < ActiveRecord::Base
  has_many :conversation_users, :dependent => :destroy
  has_many :conversations, :through => :conversation_users
end

class Conversation < ActiveRecord::Base
  has_many :conversation_users, :dependent => :destroy
  has_many :users, :through => :conversation_users
end

class ConversationUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
end
Given that, what, Nolgthorn, are you storing in the conversation_users table beyond your foreign keys? Let's say, for example, that you had a visible flag that you were trying to access. You could do something like this:

code:
class User < ActiveRecord::Base
  has_many :conversation_users, :dependent => :destroy
  has_many :conversations, :through => :conversation_users,
    :select => 'conversations.*, conversation_users.visible'
end
Which would allow you to access the visible field from the conversation model.

code:
@conversations = current_user.conversations
@conversations.any? { |conversation| conversation.visible }
I am afraid I do not have a real good feel for what you are trying to do with the join record, so this may or may not make sense in your application.

skidooer fucked around with this message at 05:43 on Mar 13, 2009

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

skidooer posted:

The "Rails Way" would be to use has_many :through, which is essentially what he is doing, save actually defining the through association.

If I'm understanding his models correctly, you couldn't really use has_many :through in this situation. I think what is going on is that a user can "own" a conversation (they started it, presumably), and that each conversation has an owner (what user started it). If this is not the case, then things change drastically. However, I have been working off of this assumption


Nolgthorn posted:

that is what im doing.

The problem is i want to eagerly load the table for items on the index, which isn't really a problem... just a concern that it's not the best way to do it. Thats all, and Im obviously doing something that isnt common after all so no worries.

:(

I think in order to eagerly load your ConversationUser, you'd have to do this in your controller:

If you want to know about only the currently logged in user:
code:
Conversation.find(:all, :conditions => {:user_id => current_user.id}, :include => [:conversation_users])
If you want to know about all users:
code:
Conversation.find(:all, :include => [:conversation_users])

skidooer
Aug 6, 2001

Operation Atlas posted:

If I'm understanding his models correctly, you couldn't really use has_many :through in this situation. I think what is going on is that a user can "own" a conversation (they started it, presumably), and that each conversation has an owner (what user started it). If this is not the case, then things change drastically. However, I have been working off of this assumption
I think what is confusing here is that the domain model isn't quite right. If we move the ownership into the join model, you can get the owner by the following, while still maintaining the has_many :through relationship:
code:
class Conversation < ActiveRecord::Base
  has_many :conversation_users
  has_many :users, :through => :conversation_users
  has_one :owner, :through => :conversation_users, :source => :user,
    :conditions => 'conversation_users.owner = 1'
end
But maybe I am missing something.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
It isn't the owner... I think we're falling way off base here of what I've been trying to do, though skidooer's initial suggestion was good.

Remember that the conversation_users table is there to keep track of the last time a user visited a given conversation and some assorted other info that relates the two. But I guess what I didn't clarify was that a record gets added or updated on this table once every time any user visits a conversation. There is one conversation_user record for each pairing of a conversation and a user, where the user has visited the conversation.

The solution I am using based on Operation Atlas' first suggestion works, but I was aware that Active Record was capable of searching based on two indexes because I know of has_many through and I know of has_and_belongs_to_many...

That's all I really wanted was to pair up the current user and a list of conversation objects then be able to eagerly load the conversation_user data for each conversation where available.

Pardot
Jul 25, 2001




Nolgthorn posted:

Remember that the conversation_users table is there to keep track of the last time a user visited a given conversation and some assorted other info that relates the two

In this case, you really should make it a join model like skidooer suggested. Espically since you're saying that there'll be other assorted info.

Instead of ConversationUser, call it something like ConversationViewings. When a user views a conversation you can make a new one, and then keep track of all viewings, or make a class method on it that took a user and conversation and checked if there already was one, then created a new one or updated the existing one if you only wanted to store the latest.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
That's what I'm already doing though.

I really appreciate all the help with this but please start talking about something else guys. I'm already doing what people are suggesting which tells me that I'm already doing it the best way I can, or I'm doing it the way I should.

:)

Thanks to everyone though.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Ok gently caress it, here's what I did instead.

It's a tale of woe and sadness, a knight's frustration and suffering. Many castles have fallen, many people left a bitter wreck at the side of the road.

The amount of complication that had arisen out of all this became too much for my little medieval brain to bear, you're just going to have to trust me that it started to get pretty crazy and all for what I kept asking myself.

So I've exposed my current_user object to the model using cattr_accessor, which is something yes I know I am not supposed to do.
code:
class User < ActiveRecord::Base
  has_many :conversations, :dependent => :nullify
  has_many :conversation_users, :dependent => :destroy
  acts_as_authentic
  cattr_accessor :current_user
end

class ApplicationController < ActionController::Base
  before_filter :put_current_user_into_model

  private
    def put_current_user_into_model
      User.current_user = current_user
    end
end

class Conversation < ActiveRecord::Base
  belongs_to :user
  has_many :conversation_users, :dependent => :destroy
  has_one :conversation_user,
      :conditions => ["user_id = ?", (User.current_user ? User.current_user.id : nil)]
end

class ConversationsController < ResourceController::Base
  private
  def collection
    @collection ||= end_of_association_chain.find(:all, :include => [:user, :conversation_user])
  end
end
This works like an absolute dream, I could not be happier, it's about 100% workload off my mind and you'll have to trust me on that. OOP and VMMPY and OSYYTR all be damned, this is too elegant to not use.

This is just so much off my mind. :knight:

skidooer
Aug 6, 2001

Nolgthorn posted:

This works like an absolute dream, I could not be happier
Have you tried it in production mode yet? :psyduck:

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Nolgthorn posted:

This works like an absolute dream, I could not be happier, it's about 100% workload off my mind and you'll have to trust me on that. OOP and VMMPY and OSYYTR all be damned, this is too elegant to not use.

Sorry, I really tried to be nice about this but you seem intent on ignoring the simple solutions we keep telling you about. So I can't be nice anymore.

You better hope that nobody else has to see that code or ever maintain it, because they're going to have an absolute shitfit.

YOU'RE DOING IT WRONG. That solution violates MVC, is fragile as hell, and is way too complicated. This is the absolute farthest thing from "elegant".

If I had you fix one thing though, it would be this. Do not ever do this. Ever.

WRONG:
code:
has_one :conversation_user,
      :conditions => ["user_id = ?", (User.current_user ? User.current_user.id : nil)]
RIGHT:
code:
def conversation_user(a_user)
  @cu ||= conversation_users.find(:first, :conditions => {:user_id => a_user.id})
end
Never ever call another model on an association declaration. This will blow the gently caress up in production and, besides, is nasty. And please use the hash conditions syntax wherever possible. This "user_id = ?" stuff is completely unnecessary.

skidooer
Aug 6, 2001

Operation Atlas posted:

RIGHT:
That will query the conversation_users table for every conversation row, exactly what Nolgthorn is trying to avoid.

I would be more inclined to try something like this:
code:
class Conversation < ActiveRecord::Base
  has_many :conversation_users
  has_many :users, :through => :conversation_users
  has_one :conversation_user,
        :conditions => 'conversation_users_conversations.user_id = conversation_users.user_id'
end
code:
@user.conversations.find(:all, :include => :conversation_user)
Although I still feel that there is something fundamentally wrong with the domain model as it stands.

skidooer fucked around with this message at 18:03 on Mar 16, 2009

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

skidooer posted:

That will query the conversation_users table for every conversation row, exactly what Nolgthorn is trying to avoid.

Yeah you're right, but this was the least-bad option of him insisting to have logic in the model that doesn't belong there. In my first reply to him I suggested a load from the controller, which is still the best, easiest, fastest, and most correct way of doing it.

KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!
Just started learning Rails and came to find the thread. Looks dead in here. Am I missing a new Rails thread or are there just very few Rails guys here?

Pardot
Jul 25, 2001




KarmaticStylee posted:

Just started learning Rails and came to find the thread. Looks dead in here. Am I missing a new Rails thread or are there just very few Rails guys here?

There just aren't that many. You picked an exciting time to start learning though, rails 3 is coming out in a few months, and it's going to be awesome.

Use http://apidock.com for looking stuff up, keep your controllers small, and write tests.

KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!

Pardot posted:

There just aren't that many. You picked an exciting time to start learning though, rails 3 is coming out in a few months, and it's going to be awesome.

Use http://apidock.com for looking stuff up, keep your controllers small, and write tests.

Thanks for the greeting! And yes, I'm really excited. Reading about the Rails 3 launch is what made me take the plunge and start learning. Right now I'm reading the half-finished 3rd Edition of Agile -- at least a good portion is up-to-date with the web site having notes on parts that are not.

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
That apidock site is great. I was previously unaware of it. I find the official ruby on rails guides to be well written too. They are usually my go-to place for looking something up quickly.

http://guides.rubyonrails.org/

Anveo
Mar 23, 2002
I have a Product, and it has a single Category, and a single Subcategory. The Category model has many Subcategories. Both Category and Subcategory have a to_param method which specifies a url-safe slug.

I would like to setup a route that looks like the following:

/products/:category_slug
/products/:category_slug/:subcategory_slug

All I have been able to get so far is

/products/:category_slug/subcategory/:subcategory_slug

Is there a way to accomplish this with nested resources, or should I just create a specific route for each?

Also, I have a legacy table for related products. All it has is a left_id and a right_id. What is the best way to accomplish this in rails? I looked into nested_set and acts_as_tree stuff, but that seems like it might be a bit much for this. I could do a normal join model, but I guess what is throwing me off is both columns will be pointing to the same class (Product).

asveepay
Jul 7, 2005
internobody

Anveo posted:

Also, I have a legacy table for related products. All it has is a left_id and a right_id. What is the best way to accomplish this in rails? I looked into nested_set and acts_as_tree stuff, but that seems like it might be a bit much for this. I could do a normal join model, but I guess what is throwing me off is both columns will be pointing to the same class (Product).

I do something similar to this in one of my projects using Has And Belongs To Many. There is a table which contains user_id & community_id. Both have has_an_belongs_to_many set for each other in the model.

the tricky part for me was that normal table creation also adds an id column which had to be removed for it to work correctly, but otherwise it does exactly what you expect: for each user_id, there are X number of communities, and vice-versa.

on the other hand you are linking the same model to itself, which is slightly different, but still might work.

another option might be to create a new attribute of the product and use serialize to make an array of the values in it. then you can just push in new ids for related products

dustgun
Jun 20, 2004

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

Anveo posted:

Also, I have a legacy table for related products. All it has is a left_id and a right_id. What is the best way to accomplish this in rails? I looked into nested_set and acts_as_tree stuff, but that seems like it might be a bit much for this. I could do a normal join model, but I guess what is throwing me off is both columns will be pointing to the same class (Product).


I had an issue that might be similar - I honestly can't quite tell for sure - that was answered in this thread a while ago.

http://forums.somethingawful.com/showthread.php?threadid=2585949&pagenumber=20&perpage=40#post351829056

Anveo
Mar 23, 2002

dustgun posted:

I had an issue that might be similar - I honestly can't quite tell for sure - that was answered in this thread a while ago.

http://forums.somethingawful.com/showthread.php?threadid=2585949&pagenumber=20&perpage=40#post351829056

Awesome, that worked! Although I had to stare at it for about 5 minutes to understand what was going on.

Adbot
ADBOT LOVES YOU

Lamont Cranston
Sep 1, 2006

how do i shot foam
I'm trying to implement custom 404 pages using method_missing in my ApplicationController. This code:
code:
  def method_missing(methodname, *args)
    begin
      super
    rescue ActionView::MissingTemplate
      render 'error/index', :status => 404
    end
  end
should do the trick but it throws "ArgumentError (no id given)" when it calls super. I know this can happen if I modify the value of "methodname" but I don't. Any ideas?

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