0

How would you implement a method that searches a table based on the requesting model?

I've got a User and Vehicle table which are both assigned equipment stored in an Equipment table. I'm trying to show the equipment assigned to a User and Vehicle using the same method. The Equipment table has a user_id and vehicle_id showing where equipment is assigned to.

Is there any way to pass in a table variable (which would be either user or vehicle) and then have that variable_id: as the search criteria in order to either show equipment assigned to a user or vehicle depending on which show page is being viewed-

def equip(table, id)
  equip = Equipment.where(table_id: id)
end

such that the table variable passed in replaced the "table" piece of the where() method, making it either user_id or vehicle_id?

gr0k
  • 789
  • 2
  • 9
  • 22

2 Answers2

1

As MrYoshiji said this is possible but I would highly recommend looking into polymorphic associations instead.

Jay Truluck
  • 1,509
  • 10
  • 17
  • Dunno why I didn't just think of that to begin with. Am I understanding it correctly that I could then just use @vehicle.equipments/@user.equipments to show everything assigned? – gr0k Apr 29 '13 at 21:49
  • @SamThode yup here is a railscast as well about it that may explain it a bit clearer http://railscasts.com/episodes/154-polymorphic-association-revised note it is a revised episode so you need a subscription. There is also an older one that generally covers the principals for free as well. – Jay Truluck Apr 29 '13 at 22:18
  • I was actually just looking through the old one. On that note though...and I'm wondering if I should have asked this before..with polymorphic can I have the same piece of equipment assigned to both a user and a vehicle? – gr0k Apr 29 '13 at 22:29
  • Sure, the only thing that you need to be aware of is that you need to assign this `equipment` to both. An even more logical way to do this yet would be to combine a `has_many :through` with the polymorphic association. This is of course a bit more confusing to construct but if you want for example user 1 to have equipment 1 and vehicle 1 to have equipment 1 while still maintaining the ease provided by the polymorphic association this would be the route to go. Here is a fairly general example:http://stackoverflow.com/questions/4641500/rails-polymorphic-has-many-through – Jay Truluck Apr 29 '13 at 22:54
0

Yes, it is possible:

def equip(table, id)
  equip = Equipment.where("#{table}_id = ?", id)
end

But this could lead to errors quickly, add some check stuff before:

def equip(table, id)
  if self.columns.map(&:name).include?("#{table}_id")
    equip = Equipment.where("#{table}_id = ?", id)
  else
    # the columns does not exists!
  end
end

Explanation:

self.columns.map(&:name) # returns the name of all the columns of the model in a array

An example with my Rails console:

User.columns.map(&:name)
=> ["id", "username", "profession_id", "external_reference", "authorisation_group", "created_at", "updated_at", "active", "license", "visible"]
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117