Odd behaviour when extending an object with a module in ruby - ruby-on-rails

If I do the following:
user = User.new
user.extend Customer
user.is_a? Customer == true
it works fine.
However if I do the following (where Profile is a mongoid model and user is embedded):
profile = Profile.all.first
profile.user.extend Customer
profile.user.is_a? Customer == false
user is no longer a customer. There must be a simple reason for this but I can't see it.
Edit: User and Profile are mongoid Models, ie. profile.user.class = User and profile.class = Profile.

My guess: every time you call profile.user you are getting a new object. Try:
2.times{ p profiler.user.object_id }
and see what happens. Then try:
u = profile.user
u.extend Customer
p u.is_a? Customer
p profile.user.is_a? Customer
and see what output you get.

This actually ended up being a bug in Mongoid. Here is the issue with description:
https://github.com/mongoid/mongoid/issues/1933
It is now fixed in the latest commit.

Related

attr_accessor not updating value from rails model

I have the following model
class Job < ActiveRecord::Base
attr_accessor :incentive
end
I want to be able to store a temporary column in my model via attr_accessor.
I want to be able to do something like this
job = Job.last
job.incentive = {id: 1}
and i expect if i do job.incentive, it should return {id: 1}
I also tried doing this as well
def incentive =(val)
#incentive = val
end
def incentive
#incentive
end
But that also didn't work. How can i be able to store temporary column values in rails 4
You script is fine, you'll find the below script working perfectly in your rails console:
job = Job.last
job.incentive = { id: 1 }
p job.incentive # returns { id: 1 }
If you restart or refresh your console (or webpage) this information is gone, since it is only set in memory and not stored to the database.

Find active record on the bases of any other id

I want to find records on the bases of demo_id from Demojobs model.
def show
#demojob = Demojob.find params[:demo_id] #instead of params[:id]
end
but it shows a error Couldn't find Demojob without an ID
Use .find_by
#demojob = Demojob.find_by(demo_id: params[:demo_id])
EDIT:
For Rails version lower than 4.0.2, use:
#demojob = Demojob.where(demo_id: params[:demo_id]).first

How to access to the company name into an email in rails?

I'm currently trying to use the company name present into an email. But I don't find a simple way to access to. And ask i there is others.
An example is better than explanation :
User.new (mail) => user#company1.com
--> That the value that I want to catch <--
==> #company = company1
if Company.where(name: #company).any?
render show #company
else
reder new Company
end
So if you have any solutions to access to that, you'll be my hero !
Thanks for your time
I'm not an expert on rails, but you can try this :
#pars1 = #company.split('#')
#pars2 = #pars[1].split('.')
#pars3 = #pars[0]
=> company
Try to read that, it can be useful :
How can you return everything after last slash(/) in a Ruby string

Changing Spree from_address

I am trying to change Spree 3.0 from_email
I added this line to my spree initialiser, but it does not work:
Spree::Store.current.mail_from_address = “x#x.com"
Do you know of any reason why not?
I also put it directly in my mailer decorator:
Spree::OrderMailer.class_eval do
def confirm_email_to_store(order, resend = false)
Spree::Store.current.mail_from_address = "x#x.com"
#order = order.respond_to?(:id) ? order : Spree::Order.find(order)
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
subject += "#{'Will Call' if #order.will_call} #{'Just to See' if #order.just_to_see} ##{#order.number}"
mail(to: ENV['STORE_EMAIL'], from: from_address, subject: subject)
end
end
This also did not work
Check you might have created multiple stores via checking Spree::Store.all
Also, spree use current store as store which updated last so you have to check that also
You can simply change the from email address in the Admin Panel under Configuration -> General settings:
Got it to work this way:
Spree::Store.current.update(mail_from_address: ENV["STORE_EMAIL"])
Looking here http://www.davidverhasselt.com/set-attributes-in-activerecord/ you can see that:
user.name = "Rob"
This regular assignment is the most common and easiest to use. It is
the default write accessor generated by Rails. The name attribute will
be marked as dirty and the change will not be sent to the database
yet.
Of course, spree initializers claims to do save in the databse, but it did not:
If a preference is set here it will be stored within the cache & database upon initialization.
Finally calling Spree::Store.current will pull from the database, so any unsaved changes will be lost:
scope :by_url, lambda { |url| where("url like ?", "%#{url}%") }
def self.current(domain = nil)
current_store = domain ? Store.by_url(domain).first : nil
current_store || Store.default
end
Fixing this bug in Spree would be prefrable, this is sort of a workaround

Displaying from my database

I'm new to rails and I'm working on a project where I'm having an issue. I'm trying to display all the gyms that have the same zipcode. When I tried the code below, it only displays 1 and not the other ones. How can display all the gym that have the same zip code?
controller
def gym
#fitness = Fitness.find_by(zip_code: params[:zip_code])
end
gym.html.erb
<%= #fitness.name %>
You're doing this to yourself. By definition, #find_by only returns a single record, or nil. You probably want #where instead:
Fitness.where(zip_code: params[:zip_code])
If that still doesn't work, check both your table data and the content of your params hash to make sure you're creating a valid query.
def gyms
#fitness = Fitness.where("zip_code = ?", params[:zip_code])
end

Resources