Rails 3, Has_One/Has_Many With Lambda Condition

Rails 3, has_one / has_many with lambda condition

*Hmm I'm not sure I understood your question but this may help you:

# code
class Topic < ActiveRecord::Base
scope :for_user, lambda { |user| includes(:bookmarks).where(bookmarks: { user_id: user.try(:id) || user} ) }

# call
Topic.for_user(current_user) # => Array of Topics

As you can see the parameter of the scope for_user can be a User object OR a user id.

Hope this helps!

Similar questions:

  • How to query a model based on attribute of another model which belongs to the first model?
  • Rails active record querying association with 'exists'
  • Rails 4 scope to find parents with no children
  • Join multiple tables with active records

Rails has_many with dynamic conditions

Rails 4+ way (Thanks to Thomas who answered this below):

has_many :faixas_aliquotas, -> (object) { 
where("regra_fiscal = ?", object.regra_fiscal)
},
:class_name => 'Fiscal::FaixaAliquota'

Rails 3.1+ way:

has_many :faixas_aliquotas, :class_name => 'Fiscal::FaixaAliquota',
:conditions => proc { "regra_fiscal = #{self.regra_fiscal}" }

Rails 3 and below:

has_many :faixas_aliquotas, :class_name => 'Fiscal::FaixaAliquota',
:conditions => ['regra_fiscal = #{self.regra_fiscal}']

No. This is not a mistake. The conditions are specified in single quotes and still contains the code #{self.regra_fiscal}. When the conditions clause is evaulated, the regra_fiscal method will be called on the object of self (whatever the class is). Putting double quotes will not work.

I hope this is what you are looking for.

How to create a relationship with lambda in rails 4.0.x?

In Rails 4, the lambda scope -> { ... } needs to be defined before any other options. So, try:

belongs_to :project,
-> { where("collaborations.collaboratable_type = 'Project'")},
:foreign_key => 'collaboratable_id'

How to validate_presence_of with condition in a nested attributes?

I've done this before, although I've not got the code handy right now:


Nested

From what I remember, you can actually put the validation in the nested model:

#app/models/question.rb
class Question < ActiveRecord::Base
validates :answer_field, presence: true, if: lambda {isrequired == true}
end

I highly recommend using the new validates syntax

--

inverse_of

I'm sure I had to use inverse_of somewhere in the code I had (it's locked in a private GitHub repo sorry).

inverse_of basically includes the associated model in your current model. Much like how you've found the effectiveness of self.question.isrequired:

#app/models/answer_detail.rb
class AnswerDetail < ActiveRecord::Base
belongs_to :question, inverse_of: :answer_detail
validates :answer_field, presence: true, if: lambda { question.isrequired == true }
end

#app/models/question.rb
class Question < ActiveRecord::Base
has_one :answer_detail, inverse_of: :question
end

However, good news for you:

enter image description here

Ownership conditional in a belongs_to association

First, there is probably an error in your associations, cause it seems like you need a join table for the participants relationship.
You should probably use a http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
has_many through association.

Something like this :

class User < ActiveRecord::Base
has_one :owned_post, class_name: "Post", foreign_key: :owner_id
has_many :participations
has_many :posts, through: :participations
end

class Participation < ActiveRecord::Base
belongs_to :post
belongs_to :participant, class_name: "User"
end

class Post < ActiveRecord::Base
belongs_to :owner, class_name: "User"
has_many :participants, through: :participations
end

When you have this model, you can use a validation on the participation model to prevent an owner to be a participant. By using a custom validation : http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations

class Participation < ActiveRecord::Base
belongs_to :post
belongs_to :participant, class_name: "User"
validate :participant_is_not_the_owner

def participant_is_not_the_owner
if participant == post.owner
errors.add(:participant, "can't be the owner")
end
end
end

Rails scope filtering elements with no has_many associated elements

You can do the following:

class Property < ActiveRecord::Base
has_many :photos
scope :has_no_photo, includes(:photos).where(photos: { id: nil })
scope :has_photo, includes(:photos).where('photos.id IS NOT NULL')
# rails 4 or higher (thanks to @trip)
scope :has_photo, includes(:photos).where.not(photos: { id: nil })

Similar questions:

  • How to query a model based on attribute of another model which belongs to the first model?
  • Rails active record querying association with 'exists'
  • Rails 3, has_one / has_many with lambda condition
  • Join multiple tables with active records
  • Rails 4 scope to find parents with no children

Rails has_many conditions

Use single quotes to enclose the condition:

class Profile < ActiveRecord::Base
has_many :friendships
has_many :removed_friends, :class_name => 'Profile', :through => :friendships,
:conditions => '
( friendships.profile_id = #{self.id} OR
friendships.friend_id = #{self.id}
) AND
(CASE WHEN friendships.profile_id=#{self.id}
THEN friendships.profile_rejected
ELSE friendships.friend_rejected
END = 1
) AND
(p.banned = 0)'
end


Related Topics



Leave a reply



Submit