Handfuls of Rails Nails #
Once we were only wading to the knee in Rails documentation. Now that the belt buckle is submerged, we all take turns excavating.
ActiveRecord::Base#constrain
I’m in the controller. I want to limit a view to a subset.
Article.constrain(:conditions => "blog_id = 1") do Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1 end
In combo with permissions, this can be great. Because constrain
can take nil
.
constraints = nil unless @user.sees_all? constraints = {:conditions => ['user_id = ?', @user.id]} end
Has anyone coupled this with destroy
to nuke a chain of associated rows?
Update: From the comments, we learn that constrain
is behind the scoped database calls.
Receiving Through ActionMailer
I want messsages to a certain e-mail address to travel through Rails. The wiki has a very thorough discussion on the topic, including many setup options.
Specifically, here’s a way to handle bounces. In other words, if an e-mail bounces to an address that’s signed up for a login, you could flag the account, giving the user a better reason why their account isn’t active.
class Inbox < ActionMailer::Base def receive(bounce) from = bounce.from.first if from =~ /MAILER-DAEMON/ bounce.each_part do |part| if part.content_type == "text/rfc822-headers" email = TMail::Mail.parse( part.body ) user = User.find_by_email( email.to.first ) user.update_attributes :disabled => true return end end end end end
It’d be good to have TMail in the API docs. But the above should give you an idea of how to parse attachments which contain email headers. And how to cycle attachments.
Forming XHTML from User Input
We’ve talked about this before, but I swear this is the shortest recipe and most cooperative. Assume you have incoming blog content HTML contained in the html_content
accessor. Also, require 'htree'
in your config/environment.rb
.
def xhtml_content HTree.parse( html_content ).display_xml( '' ) end
Marcel
contrain
is used to do the nifty newish class-methods-on-associations.constrain is not so good a name though…
Jeremy
Warning:
constrain
will likely change to something aggravating likewith_finder_options
before 1.0 to avoid stepping on the established notion of relational constraints.jvoorhis
When I first read about
#constrain
, I remember wishing#around_filter
worked more like Java’s servlet filters so that the action would execute within the block I provide. Yes, this means the action would have to be passed in and I would have to execute it manually (if I chose to). Any input?why
jvoorhis: so basically if you yield inside a controller?
MenTaLguY
Nice. You know, I keep running across that:
...pattern lately, and I keep wondering whether there is a prettier way to do it.
MenTaLguY
Oh, answering my own question.
HTree::Node#display_xml returns
out
. That means you can write:I don’t know. Is that sufficiently readable?
why
Sure, updated the snip like so. Thanks, M.guY.
drbrain
“Has anyone coupled this with destroy to nuke a chain of associated rows?”
That’s what referential integrity is for.
Danno
Referential Integrity, Smegferential Finblegrity.
Relational Databases will be first agains the wall when the revolution comes!
Jeremy
“That’s what referential integrity is for.”
And here I thought that’s what abstinence is for.
Comments are closed for this entry.