Possible to Alias a Belongs_To Association in Rails

Is it possible to alias an attribute through a belongs_to or has_one association in RoR?

I'm pretty sure you're stuck with delegate for this. The good news is you can delegate all the methods you need on a single line.

belongs_to :state

delegate :flag, :flag=, to: :state, prefix: true

You'll need to remember to save the associated state when you save the city if you're delegating flag=, though.

Belongs_to alias

I don't think as: is what you are looking for. I think your issue is similar to the issue in belongs_to with :class_name option fails

Try

# user.rb
has_many :comments, foreign_key: "commenter_id"

# comment.rb
belongs_to :commenter, class_name: "User", foreign_key: "commenter_id"

How to create an alias in a belongs_to declaration for a polymorphic relation in Ruby on Rails 5.2?

You could use alias_attribute by adding the following to your BusinessObject model:

alias_attribute :parent, :area_process

has_many, belongs_to association where has_many associated model has two alias fk in belongs_to associated_model

I believe you should use two separated associations and create a method to return all fights. Isn't it possible someday you'll need to get only the fights where some @user was the challenger?

I'd do this like the following:

class User < ActiveRecord::Base
has_many :fights_as_challenger, :foreign_key => :challenger_id,
:class_name => "Fight"
has_many :fights_as_challengee, :foreign_key => :challengee_id,
:class_name => "Fight"

def all_fights
self.fights_as_challenger + self.fights_as_challengee
end
end

Rails has_many with alias name

Give this a shot:

has_many :jobs, foreign_key: 'user_id', class_name: 'Task'

Note, that :as is used for polymorphic associations.

Also, foreign_key option for has_many.

Rails 5: Add belongs_to association with custom name to model, and migration

rails g migration add_user_to_questions user:references
rails db:migrate

Then in model:

class Question < ApplicationRecord # or ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: :user_id
end

Rails query join association table with alias

As for Rails 4.2.1, I believe you just cannot provide an alias when using joins from ActiveRecord.

If you want to query edges by the first node, you could do it just like you stated:

Edge.joins(:first).where(nodes: {value: 1})
SELECT "edges".* FROM "edges" INNER JOIN "nodes" ON "nodes"."id" = "edges"."first_id" WHERE "nodes"."value" = 1

But if you have to query using both nodes, you can still use joins like this:

Edge.joins(:first, :second).where(nodes: {value: 1}, seconds_edges: {value: 2})
SELECT "edges".* FROM "edges" INNER JOIN "nodes" ON "nodes"."id" = "edges"."first_id" INNER JOIN "nodes" "seconds_edges" ON "seconds_edges"."id" = "edges"."second_id" WHERE "nodes"."value" = 1 AND "seconds_edges"."value" = 2

Testing an association with an alias_attribute

I wouldn't recommend using alias_attribute for that purpose. As far as I'm aware, shoulda uses ActiveRecord::Reflection to investigate associations. The only thing alias_attribute does is create methods to proxy messages from the target to the origin through getter, setter and '?' methods. It was clearly intended to work with ActiveRecord attributes and not general purpose methods.

The effect of that is that alias_attribute won't register those targets as ActiveRecord associations and the current implementation of shoulda won't be able to catch them.

There are also side effects to that pattern. As you might know, when you create an association, ActiveRecord also creates helper methods to make your life easier. For instance, a belongs_to also creates:

build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})

By your example, using alias_attribute won't give you album.build_album_type and that's something other contributors may be willing to rely upon, as they expect that to be the default behavior.

The best way to handle this is precisely what you told you wouldn't like to do, using the belongs_to method to create the association under the name you really want.



Related Topics



Leave a reply



Submit