-2

I have a database with table user and table blacklist

In my user model I want to search for if the users ID exists in the blacklists table in the column legacy_logid.

So I define

def blacklist_ip
  if Blacklists.legacy_logid == user.id
    Blacklists.value
  else
    nil
  end
end

Which seems to me to be a pretty simple way of asking to check the table and see if the userID is in there. But no ..

uninitialized constant User::Blacklists

So how do I access a record in a different table in this model. The legacy_logid is a property of the table - all I want to do is look it up.

If I try with ::Blacklists I get

uninitialized constant Blacklists

If I try

def blacklist_ip
 if Blacklist.legacy_logid == user.id
Blacklist.value
 else
nil
 end
end

I get

Undefined method `legacy_logid' for #<Class:0x00000006407e40>

I have Blacklist defined as

class Blacklist < ActiveRecord::Base

end

Its not a method I want to look up -its a property and yet this seems to be only accessed in an illogical manner.

techguy
  • 47
  • 2
  • 9
  • The constant `Blacklists` cannot be resolved. Try adding a double colon in front when using it: `::Blacklists`. This will result in another error but illustrates that the autoloading does not work. My best guess is that your code will work when you write `Blacklist` (singular) instead. – Raffael Jan 19 '16 at 16:37
  • Have you defined `Blacklists` somewhere? Rails isn't magic; you can't just say a name and assume that Rails will know it's the name of a database table you want to perform a query on. To understand how Rails links classes (models) to database table, you should read the [Active Record Basics](http://guides.rubyonrails.org/active_record_basics.html) Rails Guide. – Jordan Running Jan 19 '16 at 16:38
  • What happens when you do `grep -R "Blacklists" ./app/models`? If nothing comes up, then you have not defined the class/module/constant `Blacklists`. Know that tables in Rails are plural, but they must be backed by a model that is singular. (`User` class/model, and `users` table.) – onebree Jan 19 '16 at 16:42

1 Answers1

1

First of all in rails model names are written in singular while tables are in plural.

So make sure you have a table users and another blacklists so that you can access them through User and Blacklist models respectively.

You can find whether there is a record in blacklists with a given user id by doing:

def blacklist_ip
  Blacklist.where(legacy_logid: self.id)
end

I suppose relationship between your models is User has many Blacklist while Blacklist belongs to User.

If you define your User class like below you'd be able to return all ip blacklisted of a given user:

class User < ActiveRecord::Base
  has_many :blacklists

  def blacklist_ip
    self.blacklists
  end
end
Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21
  • 1
    `Blacklist.where("legacy_logid = ?", self.id)` can be written as `Blacklist.where(legacy_log_id: id)` – spickermann Jan 19 '16 at 16:54
  • this looks great @CristianoAlencar If I did def blacklist_ip Blacklist.where(legacy_logid: self.id) end then I should then be able to get at the other columns in the table via user.blacklist_ip.other_table I would think? – techguy Jan 19 '16 at 17:10
  • @CristianoAlencar OK thats got me most of the way there - but how do I then access a column in that table. Your solution gives me the active record - so I have the row but def blacklisted_ip if blacklisted_exists.exists? blacklisted_exists.value end end then tells me no method for value. If I have the object and value is property of that object - how do I get to it (this has gotta be basic and I look like a fool!!) – techguy Jan 19 '16 at 17:34