What Is the Fully Qualified Name of a Model in Ruby on Rails

What is the fully qualified name of a model in Ruby on Rails?

Refer to them using their full names, FooModule::User and ::User

Generally if you just use User, it should assume you mean ::User, unless you are within FooModule. Either way, use FooModule::User or ::User to be sure.

What is the fully qualified namespace for an ActiveRecord model?

There isn't a namespace around ActiveRecord models loaded into a Rails project unless you explicitly declare one.

For example, if you have a User model it will not be namespaced unless declared as such.

How do I use a model that has the same name as a gem?

You can't. A module and class of the same name cannot exist at the top-level namespace. If you actually did get Rails to load your Stripe module, your app would crash with a type error, complaining that you've tried to change the type of Stripe from module to class.

Formatting Full name in Ruby on Rails

You can try this:

def name
[first_name, middle_name, last_name].select(&:present?).join(' ').titleize
end

Display full name in rails select form defined in model

You're calling :first_name for the text property. Use :full_name

<%= form.collection_select :user_id, User.all, :id, :full_name, {prompt: "Select"}, autofocus:true, class: "form-control", id: "user" %>

Ambiguity in Ruby class name resolution

Generally where there's ambiguity you specify full namespace paths:

module Foo
module Bar
class SubUser < Foo::Bar::User; end
end
end

That might seem verbose, but it's also specific and unambiguous.

Where you want to refer to literal top-level User you'd specify ::User.

How do I get the name of a Ruby class?

You want to call .name on the object's class:

result.class.name

Rails Model: Name -- First, Last

It's possible, but I wouldn't recommend it.

I would just stick with first_name and last_name if I were you and add a method fullname:

def fullname
"#{first_name} #{last_name}"
end

Edit:

If you really do want user.profile.name, you could create a Name model like this:

class Name < ActiveRecord::Base
belongs_to :profile

def to_s
"#{first} #{last}"
end
end

This allows you to do:

user.profile.name.to_s  # John Doe
user.profile.name.first # John
user.profile.name.last # Doe

Search by full name

You method client_full_name is an instance method of class Client:

def client_full_name
"#{self.Client_fname} #{self.Client_mi} #{self.Client_lname}"
end

It ( client_full_name ) is not a column(at least it doesn't appear to be) of your clients table. That's why you're getting this error:

SQLite3::SQLException: no such column: client_full_name: SELECT "clients".* FROM "clients"  WHERE (client_full_name like '%John Smith%')

to make your query work, you need to change your method search_by_client_full_name to this(NOTE- Query below will work only for MySQL):

def self.search_by_client_full_name(query)
where("CONCAT_WS(' ', Client_fname, Client_mi, Client_lname) LIKE :q", :q => "%#{query}%")
end

For SQLite you can use || for concatenation:

def self.search_by_client_full_name(query)
where("(Client_fname || Client_mi || Client_lname) LIKE :q", :q => "%#{query}%")
end

But || will return NULL if any of the column has NULL, to avoid that you'll have to write case



Related Topics



Leave a reply



Submit